[EMAIL PROTECTED] wrote:
> Fresh copies of class vars so the first one is the correct: ('foo',
> 'bar', [], False)
Ahh, yeah, then you definitely need the copy.copy call.
import copy
class ClassVars(type):
> ... def __init__(cls, name, bases, dict):
> ... for name, valu
Sorry for not being clear.
Fresh copies of class vars so the first one is the correct: ('foo',
'bar', [], False)
>>> import copy
>>>
>>> class ClassVars(type):
... def __init__(cls, name, bases, dict):
... for name, value in type(cls).classVars.iteritems():
... if name not
[EMAIL PROTECTED] wrote:
> Oops! This isn't working. As the sequence I'm trying for is
def set_classvars(**kwargs):
> ... def __metaclass__(name, bases, classdict):
> ... for name, value in kwargs.iteritems():
> ... if name not in classdict:
> ...
Oops! This isn't working. As the sequence I'm trying for is
>>> def set_classvars(**kwargs):
... def __metaclass__(name, bases, classdict):
... for name, value in kwargs.iteritems():
... if name not in classdict:
... classdict[name] = value
...
Much better. Thanks again.
--
http://mail.python.org/mailman/listinfo/python-list
[EMAIL PROTECTED] wrote:
> OK no question. I'm only posting b/c it may be something another newbie
> will want to google in the future. Now that I've worked thru the
> process this turns out to be fairly easy.
>
> However, if there are better ways please let me know.
>
> Module = ClassVars.py
>
OK no question. I'm only posting b/c it may be something another newbie
will want to google in the future. Now that I've worked thru the
process this turns out to be fairly easy.
However, if there are better ways please let me know.
Module = ClassVars.py
import copy
class ClassVars(type):
c
If some one ever wants to build on this in the future, the current form
and use is:
import copy
class ClassVars(type):
classVars = dict(fields=[], longest=0) # adjust this
def __init__(cls, name, bases, dict):
for name, value in ClassVars.classVars.iteritems():
Hmm. setattr() only does a shallow search. Good to know.
Your
if not name in dict: setattr(cls, name, value)
is a more succinct/better way of writing
if not cls.__dict__.has_key(var): setattr(cls, var, val)
Which i tested a fair bit.
OK it appears that both are working for the simple
[EMAIL PROTECTED] wrote:
> I want to inherit fresh copies of some class variables. So I set up a
> metaclass and meddle with the class variables there.
>
> Now it would be convenient to run thru a dictionary rather than
> explicitly set each variable. However getattr() and setattr() are out
> beca
10 matches
Mail list logo