classmethod and instance method

Xavier Morel xavier.morel at masklinn.net
Fri Feb 3 08:29:53 EST 2006


andychambers2002 at yahoo.co.uk wrote:
> Yes that's better.  Didn't know about the __class__ attribute.  I
> thought there must be a way to access this but couldn't find it in the
> docs.
> 
> Thanks,
> 
> Andy
> 
dir(), help() and the interactive interpreter (IDLE or CLI) are your 
best friends.

Any time you wonder what an object does, or if you do something to an 
object, fire the interpreter, create your object, abuse it via 
dir/help/whatever, and then use dir/help/whatever on the results of 
dir'ing your object.

Oh, and use new style classes too (explicitly inherit from `object` or a 
built-in type), they are much more flexible and interesting than 
old-style classes

 >>> class Test: # old style class
	pass

 >>> test = Test()
 >>> dir(test) # are completely uninteresting
['__doc__', '__module__']
 >>> class Test(object): # new style classes on the other hand
	pass

 >>> test = Test()
 >>> dir(test) # have 2500% more sexyness
['__class__', '__delattr__', '__dict__', '__doc__', '__getattribute__', 
'__hash__', '__init__', '__module__', '__new__', '__reduce__', 
'__reduce_ex__', '__repr__', '__setattr__', '__str__', '__weakref__']
 >>>



More information about the Python-list mailing list