Dynamic properties

michael michael.bierenfeld at web.de
Fri Jan 21 09:43:27 EST 2005


Hello again,

I have a dictionary with the following content :

'LZ': {'type': 'N', 'bytes': '00008'},
'LVI000': {'type': 'N', 'bytes': '000010'}

This could be seen as a interface description to deal with an external
program that needs a 18 Byte communication area. 8 and 18 Bytes have
to be interpreted as a "Number" String representation. eg like
'00000180000000200' means

LZ = 180
and 
LVI000 = 200

I am now thinking about doing the following

class a
   ....

   def some_crap () ...

       for key in dict.keys ():
           setattr (self, key, value)

so that

pgm = a ()
pgm.LZ = 300 

would cause the rekonstruktion of the byte field. ===>> Properties

My first try is :

fget = lambda self: mygetattr(self, attrname)
fset = lambda self, value: mysetattr (self, attrname, value)
fdel = lambda self: mydelattr(self, attrname)

# fget, fset, fdel are used to reconstruct the byte field

setattr (self, key, property (fget, fset, fdel))

:-) This inserts me

pgm.LZ and pgm.LVI000 but when trying to access

print pgm.LZ

it gives me 

<property object at 0x4028102c>

What am I doing wrong here

Regards

Michael



More information about the Python-list mailing list