simple metaclass question

Blair Hall b.hall at irl.cri.nz
Tue Nov 12 00:12:33 EST 2002


Please don't ask me why, but in trying to use metaclasses
I simplified my code to the following:

class M(object):
    def __init__(cls,name, bases,dict):
        cls.val=0

    def set(cls,n):
        cls.val = n

class A(object):
    __metaclass__ = M
    val = 3

class B(object) :
    val = 2

I was then surprised to see that
>>> A.val
0
>>> B.val
2
>>>A.set(5)
>>>A.val
5

Why is the initial A.val not 3?
How should I change the code so that it
does work that way?

I am trying to eventually create
classes that hold values and can be manipulated
by arithmetic operators.

Any pointers gratefully accepted.




More information about the Python-list mailing list