still struggling, howto use a list-element as a name ?

Patrick Mullen saluk64007 at gmail.com
Sat Jan 6 14:13:34 EST 2007


You would use setattr to bind a name that you don't know:

for pin in self.pin:
   setattr(self,pin.Name,pin.Value)

However, I'm not sure why you're using a dictionary in this way.  I don't
totally understand the context, so I may be wrong, but I would code it more
like this:

class Power_Supply(device):
   pins = {'GND':(_DIG_OUT,_par2),'VCC':(_DIG_OUT,_par33)}
   def __init__(self):
      for pin in pins.items():   #pin will be key,value
         setattr(self,pin[0],pin[1])

or even:

class Power_Supply(device):
   pins = {'GND':(_DIG_OUT,_par2),'VCC':(_DIG_OUT,_par33)}
   def __init__(self):
      self.__dict__.update(pins)
#self.__dict__ is the name mapping for any object, update will insert any
new values from a new dictionary into the old one
-------------- next part --------------
An HTML attachment was scrubbed...
URL: <http://mail.python.org/pipermail/python-list/attachments/20070106/dcc17142/attachment.html>


More information about the Python-list mailing list