simple metaclass question

sismex01 at hebmex.com sismex01 at hebmex.com
Tue Nov 12 09:02:32 EST 2002


> From: Blair Hall [mailto:b.hall at irl.cri.nz]
> Sent: Monday, November 11, 2002 11:13 PM
> To: python-list at python.org
> Subject: simple metaclass question
> 
> 
> Please don't ask me why, but in trying to use metaclasses
> I simplified my code to the following:
> 

This is the problem: you're deriving your metaclass
from 'object', instead of 'type'.

> 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
> 

Good luck, I'm barely learning these ropes too. :-)

-gustavo




More information about the Python-list mailing list