[Tutor] Putting a variable into a statement
Dave Angel
davea at ieee.org
Fri Oct 16 12:44:18 CEST 2009
bob gailer wrote:
> GoodPotatoes wrote:
>> <snip>:
>> for x in myuser.properties: # "myuser.properties" returns a tuple
>> of properties associated with the myuser AD object
>> print myuser.x
>>
>> Traceback (most recent call last):
>> File "<pyshell#148>", line 2, in <module>
>> print myuser.x
>> File "build\bdist.win32\egg\active_directory.py", line 421, in
>> __getattr__
>> raise AttributeError
>> AttributeError
>>
>
> print getattr(myuser ,x)
>
>>
>> example:
>> >>> myuser.properties[0] #first value in user tuple
>> u'cn'
>> >>> myuser.cn
>> u'Joe Sixpack'
>>
>> How can I iterate through all of the values of the properties tuple
>> and get each value?
>>
>
I haven 't downloaded your module active_directory, (
http://timgolden.me.uk/python/active_directory.html )
so I'll just post something that works for me (python 2.6.2), and you
can check if it works for you as well.
class MyClass(object):
pass
obj = MyClass()
obj.xyzzy = 44
obj.label = "text"
#This displays attributes and values for a lot more than what you want
here. But it can be useful.
for att in dir(obj):
print att, "---", getattr(obj, att)
print "-----------"
#This is probably what you're asking about. Use __dict__ to get the
attribute names, and then getattr() to get their values.
for att in obj.__dict__:
print att, "---", getattr(obj, att)
#or even
for att, value in obj.__dict__.items():
print att, "---", value
HTH
DaveA
More information about the Tutor
mailing list