passing variables as object attributes

Jean-Michel Pichavant jeanmichel at sequans.com
Mon Aug 16 12:20:51 EDT 2010


Vikas Mahajan wrote:
> On 16 August 2010 19:23, Nitin Pawar <nitinpawar432 at gmail.com> 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



More information about the Python-list mailing list