[Tutor] Class Attribute "Overloading?"

Alan Gauld alan.gauld at btinternet.com
Wed Aug 15 20:28:27 CEST 2007

"Vincent Gulinao" <vincent.gulinao at gmail.com> wrote

> Is there any way a reference to a class attribute 
> ([class].[attribute]) be
> treated like a method ([class].[method]())?

Yes, classes are just objects and their attributes are too.

class Test: pass

def f(self): print 'howdy!'

Test.foo = f
t = Test()  # creae an instance
t.foo()   # access the class method

Creating actual class methods at runtime and calling them
via the class is something I haven't tried but it might be

Yep it works:

class Test: pass

Test.foo = staticmethod(lambda: 'hello')


Alan Gauld
Author of the Learn to Program web site

More information about the Tutor mailing list