simple metaclass question
Michael Hudson
mwh at python.net
Tue Nov 12 10:16:08 EST 2002
Blair Hall <b.hall at irl.cri.nz> writes:
> 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
/Usually/ metaclasses want to inherit from type... not sure what
you're trying to achieve here, so I might be wrong.
> 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?
Because you smashed it in M.__init__?
> How should I change the code so that it
> does work that way?
Check the ns argument for a key 'val'?
> I am trying to eventually create
> classes that hold values and can be manipulated
> by arithmetic operators.
>
> Any pointers gratefully accepted.
HTH,
M.
--
This is the fixed point problem again; since all some implementors
do is implement the compiler and libraries for compiler writing, the
language becomes good at writing compilers and not much else!
-- Brian Rogoff, comp.lang.functional
More information about the Python-list
mailing list