python:操作类的 __dict__

发布于 2024-09-18 06:34:13 字数 557 浏览 2 评论 0原文

(全部在 ActivePython 3.1.2 中)

我尝试更改类(而不是实例)属性。元类的 __dict__ 似乎是完美的解决方案。但当我尝试修改时,我得到:

类型错误:“dict_proxy”对象确实 不支持项目分配

为什么,我能做什么?

编辑

我在类定义中添加属性。

setattr 不起作用,因为该类尚未构建,因此我还无法引用它(或者至少我不知道如何引用)。

传统的分配不起作用,因为我添加了大量属性,这些属性的名称是由特定规则确定的(所以我不能只是将它们键入)。

换句话说,假设我希望类 A 具有属性 A.a001A.a999;所有这些都必须在完全构建之前定义(否则 SQLAlchemy 将无法正确检测它)。

另请注意,我在原始标题中犯了一个拼写错误:我想要修改的是常规类的 __dict__ ,而不是元类。

(All in ActivePython 3.1.2)

I tried to change the class (rather than instance) attributes. The __dict__ of the metaclass seemed like the perfect solution. But when I tried to modify, I got:

TypeError: 'dict_proxy' object does
not support item assignment

Why, and what can I do about it?

EDIT

I'm adding attributes inside the class definition.

setattr doesn't work because the class is not yet built, and hence I can't refer to it yet (or at least I don't know how).

The traditional assignment doesn't work because I'm adding a large number of attributes, whose names are determined by a certain rule (so I can't just type them out).

In other words, suppose I want class A to have attributes A.a001 through A.a999; and all of them have to be defined before it's fully built (since otherwise SQLAlchemy won't instrument it properly).

Note also that I made a typo in the original title: it's __dict__ of a regular class, not a metaclass, that I wanted to modify.

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(3

无言温柔 2024-09-25 06:34:13

遵循某些规则创建大量属性听起来像是有严重错误。我会回去看看是否有更好的方法。

话虽如此,这里有“邪恶代码”(但我认为它会起作用),

class A:
    locals()['alpha'] = 1

print A.alpha

这是有效的,因为在定义类时,有一个字典可以跟踪您正在定义的局部变量。这些局部变量最终成为类属性。对当地人要小心,因为它不一定会“正确”行事。您实际上不应该修改本地变量,但当我尝试时它似乎确实有效。

The creation of a large number of attributes following some rule smells like something is seriously wrong. I'd go back and see if there isn't a better way of doing that.

Having said there here is "Evil Code" (but it'll work, I think)

class A:
    locals()['alpha'] = 1

print A.alpha

This works because while the class is being defined there is a dictionary that tracks the local variables you are definining. These local variables eventually become the class attributes. Be careful with locals as it won't necessarily act "correctly." You aren't really supposed to be modifying locals, but it does seem to work when I tried it.

ㄟ。诗瑗 2024-09-25 06:34:13

不要使用声明性语法,而是单独构建表,然后在其上使用映射器。请参阅http://www.sqlalchemy.org/docs/05/ormtutorial.html# 我认为在定义类时没有什么好的方法可以将计算属性添加到类中。

或者,我不知道这是否会起作用,但是:

class A(object):
    pass
A.all_my_attributes = values

class B(declarative_base, A):
   pass 

可能会起作用。

Instead of using the declarative syntax, build the table seperately and then use mapper on it. see http://www.sqlalchemy.org/docs/05/ormtutorial.html# I think there is just no good way to add computed attributes to class while defining it.

Alternatively, I don't know whether this will work but:

class A(object):
    pass
A.all_my_attributes = values

class B(declarative_base, A):
   pass 

might possibly work.

揪着可爱 2024-09-25 06:34:13

我不太熟悉 3 如何处理 dict 但你也许可以通过简单地继承字典类来绕过这个问题,如下所示:

class A(dict):
 def __init__(self,dict_of_args):
  self['key'] = 'myvalue'
  self.update(dict_of_args)
  # whatever else you need to do goes here...

A() 可以像这样引用:

d = {1:2,3:4}
obj = A(mydict)
print obj['test'],obj[3]  # this will print myvalue and 4

希望这会有所帮助。

I'm not too familiar with how 3 treats dict but you might be able to circumvent this problem by simply inheriting the dictionary class like so:

class A(dict):
 def __init__(self,dict_of_args):
  self['key'] = 'myvalue'
  self.update(dict_of_args)
  # whatever else you need to do goes here...

A() can be referenced like so:

d = {1:2,3:4}
obj = A(mydict)
print obj['test'],obj[3]  # this will print myvalue and 4

Hope this helps.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文