Metaclasses and class variables
- From: Jan-Ole Esleben <esleben@xxxxxxxxx>
- Date: Thu, 4 Aug 2005 14:45:52 +0200
Hi!
I am new to this list, and maybe this is a stupid question, but I
can't seem to find _any_ kind of answer anywhere.
What I want to do is the following:
I want to insert a class variable into a class upon definition and
actually use it during definition.
Manually, that is possible, e.g.:
class A:
classvar = []
classvar.append(1)
classvar.append(2)
I don't want to explicitly set the variable, though. My idea was to
write the following:
class Meta(type):
def __new__(cls, name, bases, d):
d['classvar'] = []
return type.__new__(cls, name, bases, d)
class Test:
__metaclass__ = Meta
classvar.append(1)
classvar.append(2)
However, Python complains that the variable isn't defined; it can be
found in the class dictionary _after_ definition, though, and then it
can also be used. But where's the conceptual difference (to the manual
approach)?
Thanks in advance,
Ole
.
- Follow-Ups:
- Re: Metaclasses and class variables
- From: Thomas Heller
- Re: Metaclasses and class variables
- From: Christopher Subich
- Re: Metaclasses and class variables
- Prev by Date: Re: Python's CSV reader
- Next by Date: Re: PyGTK or wxPython (not a flame war) on Windows
- Previous by thread: Embedded python
- Next by thread: Re: Metaclasses and class variables
- Index(es):
Relevant Pages
|