Python类与模块属性 [英] Python Class vs. Module Attributes

查看:138
本文介绍了Python类与模块属性的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有兴趣听到有关在Python类属性的讨论。例如,什么是一个很好的用例类的属性?在大多数情况下,我不能拿出其中类别属性是preferable使用一个模块级属性的情况下。如果这是真的,那么,为什么让他们身边?

I'm interested in hearing some discussion about class attributes in Python. For example, what is a good use case for class attributes? For the most part, I can not come up with a case where a class attribute is preferable to using a module level attribute. If this is true, then why have them around?

我与他们的问题是,它几乎是太容易揍了错误的类属性的值,然后你的全球性的价值已经变成了一个本地实例属性。

The problem I have with them, is that it is almost too easy to clobber a class attribute value by mistake, and then your "global" value has turned into a local instance attribute.

欢迎您将如何处理以下情况发表意见:

Feel free to comment on how you would handle the following situations:


    一类和/或子类中使用
  1. 的常量。这可能包括神奇数字字典键或索引功能,将永远不会改变,但可能需要一次性初始化。

  2. 默认类属性,在极少数情况下更新类的特例。

  3. 用于重新present所有实例之间共享一个类的内部状态的全局数据结构。

  4. 是初始化一些默认属性,而不是由构造函数的参数影响的类。

一些相关文章:结果
<一href=\"http://stackoverflow.com/questions/207000/python-difference-between-class-and-instance-attributes\">Difference类之间的属性和实例属性

推荐答案

4:
我的从不的使用类属性初始化默认实例属性(你通常放在 __ __的init 的那些)。例如:

#4: I never use class attributes to initialize default instance attributes (the ones you normally put in __init__). For example:

class Obj(object):
    def __init__(self):
        self.users = 0

和永不:

class Obj(object):
    users = 0

为什么呢?因为它是不一致的:它不会做你想要什么,当你分配什么,但一个不变的目标:

Why? Because it's inconsistent: it doesn't do what you want when you assign anything but an invariant object:

class Obj(object):
    users = []

使用户列出要在所有的对象,而在这种情况下,不想要的共享。这是混淆在 __的init __ 这些分成类属性和任务根据不同的类型,所以我总是把他们都在 __的init __ ,我觉得这更清楚的呢。

causes the users list to be shared across all objects, which in this case isn't wanted. It's confusing to split these into class attributes and assignments in __init__ depending on their type, so I always put them all in __init__, which I find clearer anyway.

至于其他的,我一般写在类中类特定的值。这与其说是因为全局是邪恶 - 他们没有这么大的交易在一些语言,因为他们仍然作用域模块,除非该模块本身是太大了 - 但如果外部code要访问它们,这是方便有所有相关值在一个地方。例如,在module.py:

As for the rest, I generally put class-specific values inside the class. This isn't so much because globals are "evil"--they're not so big a deal as in some languages, because they're still scoped to the module, unless the module itself is too big--but if external code wants to access them, it's handy to have all of the relevant values in one place. For example, in module.py:

class Obj(object):
    class Exception(Exception): pass
    ...

和则:

from module import Obj

try:
    o = Obj()
    o.go()
except o.Exception:
    print "error"

除了允许子类改变值(并不总是反正想),这意味着我不必费力地导入异常名称和使用的OBJ需要一堆其他的东西。 从模块导入OBJ,ObjException,...得到迅速讨厌

Aside from allowing subclasses to change the value (which isn't always wanted anyway), it means I don't have to laboriously import exception names and a bunch of other stuff needed to use Obj. "from module import Obj, ObjException, ..." gets tiresome quickly.

这篇关于Python类与模块属性的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆