About getattr()
Duncan Booth
duncan.booth at invalid.invalid
Mon Feb 12 13:08:07 EST 2007
Leif K-Brooks <eurleif at ecritters.biz> wrote:
>> Why do I still need the getattr() func as below?
>>
>>>>> print getattr(os.path,"isdir").__doc__
>> Test whether a path is a directory
>
> You don't.
Correct
> getattr() is only useful when the attribute name is
> determined at runtime.
>
getattr() is useful in at least 2 other situations:
maybe you know the attribute name in advance, but don't know whether the
object has that particular attribute, then the 3 argument form of getattr
could be appropriate:
print getattr(x, 'attr', 'some default')
or if the attribute name isn't a valid Python identifier:
print getattr(x, 'class')
More information about the Python-list
mailing list