Metaclasses and class variables



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
.



Relevant Pages

  • Hattons and Ehattons - Whats the difference?
    ... I realise that this is possibly a stupid question for me to be asking, ... TIA ... Elliott ... Prev by Date: ...
    (uk.rec.models.rail)
  • Re: moving files to windows xp
    ... Sorry if this is a stupid question; I don't know much of computers. ... > just upgraded to a new computer with Win xp Pro. ... > computer says I need to format the disc. ... Prev by Date: ...
    (microsoft.public.windowsxp.hardware)
  • Re: [Dialog] How to plonk.
    ... >>> This is possibly a stupid question but can someone please tell me how to ... >>> set up the killfile in 40tude Dialog? ... Jim ... Prev by Date: ...
    (news.software.readers)
  • moving files to windows xp
    ... Sorry if this is a stupid question; I don't know much of computers. ... just upgraded to a new computer with Win xp Pro. ... computer says I need to format the disc. ... Prev by Date: ...
    (microsoft.public.windowsxp.hardware)