how can i know if a python object have a attribute such as 'attr1'?
Terry Reedy
tjreedy at udel.edu
Sat Jan 23 18:38:29 EST 2010
On 1/23/2010 10:56 AM, Arnaud Delobelle wrote:
> thinke365<thinke365 at gmail.com> writes:
>
>> for example, i may define a python class:
>> class A:
>> def sayHello():
>> print 'hello'
>>
>> a = A()
>> a.attr1 = 'hello'
>> a.attr2 = 'bb'
>>
>> b = A()
>> a.attr2 = 'aa'
>>
>> how can i know whether an object have an attribute named attr1?
>
> hasattr(a, 'attr1')
or
try: a.attr1
except AttributeError: pass
Terry Jan Reedy
More information about the Python-list
mailing list