From: thinke365 on 23 Jan 2010 10:49 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? -- View this message in context: http://old.nabble.com/how-can-i-know-if-a-python-object-have-a-attribute-such-as-%27attr1%27--tp27286937p27286937.html Sent from the Python - python-list mailing list archive at Nabble.com.
From: Arnaud Delobelle on 23 Jan 2010 10:56 thinke365 <thinke365(a)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') -- Arnaud
|
Pages: 1 Prev: iterating lists Next: how to generate random numbers that satisfy certain distribution |