Re: passing variables as object attributes
I got the concept to get and set object attributes and now can handle similar problems effectively. Thanks to all for your help. -- http://mail.python.org/mailman/listinfo/python-list
Re: passing variables as object attributes
Vikas Mahajan a écrit : On 16 August 2010 19:23, Nitin Pawar wrote: you would need to define a class first with its attiributes and then you may want to initiate the variables by calling the class initilializer Actually I have to dynamically add attributes to a object. I am writing python script for FreeCAD software. I am using loop to create multiple cylinders and I am writing following code-: cyname = "Cylinder" FreeCAD.ActiveDocument.addObject("Part::Cylinder",cyname) FreeCAD.ActiveDocument.cyname.Radius= .5 FreeCAD.ActiveDocument.cyname.Height= 10 And I am getting this error-: AttributeError: 'App.Document' object has no attribute 'cyname' Indeed. But when I use Cylinder in place of cyname, I am not getting any error. Of course. Please help. I don't have FreeCAD installed, I won't install it, and I'm not going to read FreeCAD's doc neither, but you may want to check whether FreeCAD.ActiveDocument.addObject actually returns the newly created objet (which would be a sensible behaviour). If so, you'd just have to keep a ref to this object, ie: cylinder = FreeCAD.ActiveDocument.addObject("Part::Cylinder",cyname) cylinder.Radius = 0.5 # etc Else, you can always get this ref using getattr, ie: FreeCAD.ActiveDocument.addObject("Part::Cylinder",cyname) cylinder = getattr(FreeCAD.ActiveDocument, cyname) cylinder.Radius = 0.5 # etc And while you're at it, you could save on both typing and execution time by keepin a ref to the document object itself: doc = FreeCAD.ActiveDocument for cyname in ("cylinder1, "cylinder2", "cylinderN"): doc.addObject("Part::Cylinder",cyname) cylinder = getattr(doc, cyname) cylinder.Radius = 0.5 # etc HTH -- http://mail.python.org/mailman/listinfo/python-list
Re: passing variables as object attributes
On 8/16/2010 9:40 AM, Vikas Mahajan wrote: Hello to all I am new to python. Hi, welcome to Python. Hint 1: 'Variable' is a rather loose term with many meanings. Better to think in terms of 'name' ('identifier'), which is specifically defined, and 'object'. I am facing problem to use variables as object attributes. Your problem is that you have a attribute name stored in a string object, rather than being an unquoted literal in the code. I have to use loop and dynamically add attributes to a object and for this purpose I have to use variables with object names. You have to use strings with attribute names. Not uncommon For example-: Let us say object car has an attribute engine, then varname = "engine" car.varname = "dummy value" setattr(car, varname, "dummy value") is the runtime replacement for car.engine = "dummy value" Note that 'setattr(car, "engine", "dummy value")' would work but is never needed, since if you have the attribute name itself, you just write the assignment. hasattr and getattr are similar. -- Terry Jan Reedy -- http://mail.python.org/mailman/listinfo/python-list
Re: passing variables as object attributes
@All Thanks a lot. getattr and setattr functions solved my problem. -- http://mail.python.org/mailman/listinfo/python-list
Re: passing variables as object attributes
Vikas Mahajan wrote: On 16 August 2010 19:23, Nitin Pawar wrote: you would need to define a class first with its attiributes and then you may want to initiate the variables by calling the class initilializer Actually I have to dynamically add attributes to a object. I am writing python script for FreeCAD software. I am using loop to create multiple cylinders and I am writing following code-: cyname = "Cylinder" FreeCAD.ActiveDocument.addObject("Part::Cylinder",cyname) FreeCAD.ActiveDocument.cyname.Radius= .5 FreeCAD.ActiveDocument.cyname.Height= 10 And I am getting this error-: AttributeError: 'App.Document' object has no attribute 'cyname' But when I use Cylinder in place of cyname, I am not getting any error. Please help. Thanks. obj.attribute = 5 <=> setattr(obj, "attribute", 5) obj.attribute <=> getattr(obj, "attribute") So considering your example, getattr(FreeCAD.ActiveDocument, cyname).Radius = .5 should do the trick. JM -- http://mail.python.org/mailman/listinfo/python-list
Re: passing variables as object attributes
On Aug 16, 8:40 pm, Vikas Mahajan wrote: > Hello to all > > I am new to python. I am facing problem to use variables as object > attributes. I have to use loop and dynamically add attributes to a > object and for this purpose I have to use variables with object names. > > For example-: > Let us say object car has an attribute engine, then > varname = "engine" > car.varname = "dummy value" > is giving me Error: object car does not have attribute varname. > > Please tell me how can I use variables as objects attributes. > > Thanks. I'm not a python expert, so this may not be accurate For me it is easier to define a class that represent the object, and then set attribute for the object. for example : # define a class with no attribute class Car(): pass # instance the class mycar = Car() # dynamically set attribute to the instance newattr = "engine" setattr(mycar, newattr, "dummy value") # or dynamically set attribute to the Class so every instance will inherit it classattr = "color" setattr(mycar, classattr, "yellow") But as I mention, I'm not an expert so maybe it's not a pythonic way to add attribute to an object. Cheers, -- http://mail.python.org/mailman/listinfo/python-list
Re: passing variables as object attributes
On Aug 16, 10:08 am, Vikas Mahajan wrote: > On 16 August 2010 19:23, Nitin Pawar wrote:> you > would need to define a class first with its attiributes and then you may > > want to initiate the variables by calling the class initilializer > > Actually I have to dynamically add attributes to a object. I am > writing python script for FreeCAD software. I am using loop to create > multiple cylinders and I am writing following code-: > cyname = "Cylinder" > FreeCAD.ActiveDocument.addObject("Part::Cylinder",cyname) > FreeCAD.ActiveDocument.cyname.Radius= .5 > FreeCAD.ActiveDocument.cyname.Height= 10 > > And I am getting this error-: > AttributeError: 'App.Document' object has no attribute 'cyname' > > But when I use Cylinder in place of cyname, I am not getting any error. > > Please help. > > Thanks. This might work: cyname = "Cylinder" FreeCAD.ActiveDocument.addObject("Part::Cylinder",cyname) getattr(FreeCAD.ActiveDocument,cyname).Radius= .5 getattr(FreeCAD.ActiveDocument,cyname).Height= 10 -- http://mail.python.org/mailman/listinfo/python-list
Re: passing variables as object attributes
On Aug 16, 10:08 am, Vikas Mahajan wrote: > On 16 August 2010 19:23, Nitin Pawar wrote:> you > would need to define a class first with its attiributes and then you may > > want to initiate the variables by calling the class initilializer > > Actually I have to dynamically add attributes to a object. I am > writing python script for FreeCAD software. I am using loop to create > multiple cylinders and I am writing following code-: > cyname = "Cylinder" > FreeCAD.ActiveDocument.addObject("Part::Cylinder",cyname) > FreeCAD.ActiveDocument.cyname.Radius= .5 > FreeCAD.ActiveDocument.cyname.Height= 10 > > And I am getting this error-: > AttributeError: 'App.Document' object has no attribute 'cyname' > > But when I use Cylinder in place of cyname, I am not getting any error. > > Please help. > > Thanks. -- http://mail.python.org/mailman/listinfo/python-list
Re: passing variables as object attributes
On 16 August 2010 19:23, Nitin Pawar wrote: > you would need to define a class first with its attiributes and then you may > want to initiate the variables by calling the class initilializer > Actually I have to dynamically add attributes to a object. I am writing python script for FreeCAD software. I am using loop to create multiple cylinders and I am writing following code-: cyname = "Cylinder" FreeCAD.ActiveDocument.addObject("Part::Cylinder",cyname) FreeCAD.ActiveDocument.cyname.Radius= .5 FreeCAD.ActiveDocument.cyname.Height= 10 And I am getting this error-: AttributeError: 'App.Document' object has no attribute 'cyname' But when I use Cylinder in place of cyname, I am not getting any error. Please help. Thanks. -- http://mail.python.org/mailman/listinfo/python-list
Re: passing variables as object attributes
you would need to define a class first with its attiributes and then you may want to initiate the variables by calling the class initilializer On Mon, Aug 16, 2010 at 7:10 PM, Vikas Mahajan wrote: > Hello to all > > I am new to python. I am facing problem to use variables as object > attributes. I have to use loop and dynamically add attributes to a > object and for this purpose I have to use variables with object names. > > For example-: > Let us say object car has an attribute engine, then > varname = "engine" > car.varname = "dummy value" > is giving me Error: object car does not have attribute varname. > > Please tell me how can I use variables as objects attributes. > > Thanks. > -- > http://mail.python.org/mailman/listinfo/python-list > -- Nitin Pawar -- http://mail.python.org/mailman/listinfo/python-list
passing variables as object attributes
Hello to all I am new to python. I am facing problem to use variables as object attributes. I have to use loop and dynamically add attributes to a object and for this purpose I have to use variables with object names. For example-: Let us say object car has an attribute engine, then varname = "engine" car.varname = "dummy value" is giving me Error: object car does not have attribute varname. Please tell me how can I use variables as objects attributes. Thanks. -- http://mail.python.org/mailman/listinfo/python-list