问题描述
我想继承一些类变量的新副本。所以我设置了一个
元类并插入那里的类变量。
现在通过字典而不是
运行会很方便
显式设置每个变量。但是getattr()和setattr()都是
因为他们通过类层次结构来追逐变量。
所以,我直接用cls读了字典。 __dict __。has_key(var)。
阅读有效但当我直接设置对象的字典
时:
cls .__ dict __ [var] = val
我收到以下错误:
文件Table.py,第10行,__ init__
如果不是cls .__ dict __。has_key(var):cls .__ dict __ [var] = val
TypeError:调用元类库时出错
对象不支持项目分配
有一个简单的方法吗?或者我是否坚持列出
变量每行一个?
类SetClassVars(类型):
cvars = dict( name = None,desc = None,required = True,minlen = 1,
maxlen = 25,idDown = 999999999,idNext = 0)
def __init __(cls,name ,base,dict):
如果不是cls .__ dict __。has_key(''name''):cls.name = None
如果不是cls .__ dict __。has_key( ''desc''):cls.desc =无
如果不是cls .__ dict __。has_key(''required''):cls.required = True
如果没有cls .__ dict __。has_key(''minlen''):cls.minlen = 1
如果不是cls .__ dict __。has_key(''maxlen''):cls.maxlen = 25
如果不是cls .__ dict __。has_key(''idDown''):cls.idDown = 999999999
如果不是cls .__ dict __。has_key(''idNext''):cls.idNext = 0
#循环通过字典会更方便
#for var,val在SetClassVars.cvars.iteritems()中:
#getattr()和setattr()通过MRO运行
#这不是我想要的
#if not getattr(cls,var):setattr(cls,var ,val)
#if not cls .__ dict __。has_key(var):setattr(cls,var,val)
#设置字典直接生成一个错误
#if not cls .__ dict __。has_key(var):cls .__ dict __ [var] = val
thanks
t4
这样做你想要的吗?请注意,我甚至不打扰__dict__
,因为类dict已经可用作__init__的最终参数。
无栏条真实
STeVe
[''ccccc'',''dddd'']
我对行为表示不满。
好的。因此,只有可变列表才会出现问题。我认为
错误,认为列表行为与
非变量相同。
这必须是一个新手的错误,它可能记录在某个地方。
*丁*我敢打赌,当他们定义
函数时会咬同一个人:
def myfunc(lst = []):
在简单错误时寻找复杂的元类问题
关于mutables是问题。奥卡姆对我嗤之以鼻。
谢谢。这有帮助。
t4
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
because they chase the variable thru the class hierarchy.
So, I read the dictionary directly with cls.__dict__.has_key(var).
Reading works but when I go to set the object''s dictionary directly
with:
cls.__dict__[var] = val
I get the following error:
File Table.py, line 10, in __init__
if not cls.__dict__.has_key(var): cls.__dict__[var] = val
TypeError: Error when calling the metaclass bases
object does not support item assignment
Is there an easy way around this? Or am I stuck listing out the
variables one per line?
class SetClassVars(type):
cvars = dict(name=None, desc=None, required=True, minlen=1,
maxlen=25, idDown=999999999, idNext=0)
def __init__(cls, name, bases, dict):
if not cls.__dict__.has_key(''name''): cls.name = None
if not cls.__dict__.has_key(''desc''): cls.desc = None
if not cls.__dict__.has_key(''required''): cls.required = True
if not cls.__dict__.has_key(''minlen''): cls.minlen = 1
if not cls.__dict__.has_key(''maxlen''): cls.maxlen = 25
if not cls.__dict__.has_key(''idDown''): cls.idDown = 999999999
if not cls.__dict__.has_key(''idNext''): cls.idNext = 0
# It would be more convenient to loop thru a dictionary
#for var, val in SetClassVars.cvars.iteritems():
# getattr() and setattr() run thru the MRO
# which is not what I want
#if not getattr(cls, var): setattr(cls, var, val)
#if not cls.__dict__.has_key(var): setattr(cls, var, val)
# Setting the dictionary directly generates an error
#if not cls.__dict__.has_key(var): cls.__dict__[var] = val
thanks
t4
Does this do what you want? Note that I don''t even bother with __dict__
since the class dict is already available as the final argument to __init__.
None bar True
STeVe
[''ccccc'', ''dddd'']
I get the piling on behavior.
OK. So it seems to be a problem only with the mutable list. I made the
mistake of thinking that the list behavior was the same as for
non-mutables.
This must be a newbie mistake and it is probably documented somewhere.
*Ding* I''ll bet it is the same one that bites newbies when they define
functions like:
def myfunc(lst=[]):
Looking for complicated problems with metaclasses when simple mistakes
about mutables are the issue. Occam wags his finger at me.
Thank you. That helped.
t4
这篇关于使用元类继承类变量的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!