Class's metafunction problem

Jeff Epler jepler at unpythonic.net
Fri Oct 18 07:51:05 EDT 2002


On Fri, Oct 18, 2002 at 01:24:15PM +0200, Gregor Mirai wrote:
> Is it possible that a class would have a member function, that could change
> his variables (class variables) ? Here I am not referring to instance
> variables.

class A(object):
    x = 0
    def set_x(cls, val):
	cls.x = val
    set_x = classmethod(set_x)


>>> a = A()
>>> print a.x
0
>>> A.set_x(37)
>>> print a.x
37

I think that classmethod only works with new-style classes, and only in
recent versions of python.  see the docs for more information.  see also
"staticmethod".

Jeff




More information about the Python-list mailing list