如何在 Python 中声明静态属性?
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/27481116/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
How to declare a static attribute in Python?
提问by Romulus
How can I declare a static attribute in Python?
如何在 Python 中声明静态属性?
Here is written how I can declare a method: Static methods in Python?
这里写了我如何声明一个方法: Python 中的静态方法?
采纳答案by Mir Shahriar Sabuj
All variables defined on the class level in Python are considered static
在 Python 中定义在类级别的所有变量都被认为是静态的
class Example:
Variable = 2 # static variable
print Example.Variable # prints 2 (static variable)
# Access through an instance
instance = Example()
print instance.Variable # still 2 (ordinary variable)
# Change within an instance
instance.Variable = 3 #(ordinary variable)
print instance.Variable # 3 (ordinary variable)
print Example.Variable # 2 (static variable)
# Change through Class
Example.Variable = 5 #(static variable)
print instance.Variable # 3 (ordinary variable)
print Example.Variable # 5 (static variable)
You can have two different variables in your class under the same name (one static and one ordinary). Don't be confused.
您的类中可以有两个不同的同名变量(一个是静态变量,一个是普通变量)。不要混淆。
回答by bosnjak
All variables declared inside the Class' body are 'static' attributes.
在 Class 主体内声明的所有变量都是“静态”属性。
class SomeClass:
# this is a class attribute
some_attr = 1
def __init__(self):
# this is an instance attribute
self.new_attr = 2
But keep in mind that the 'static' part is by convention, not imposed (for more details on this, read this SO thread).
但请记住,“静态”部分是约定俗成的,而不是强加的(有关这方面的更多详细信息,请阅读此 SO 线程)。
For more details of this convention and its implications, here is a quick excerpt from the official documentation:
有关此约定及其含义的更多详细信息,以下是官方文档的快速摘录:
“Private” instance variables that cannot be accessed except from inside an object, don't exist in Python. However, there is a convention that is followed by most Python code: a name prefixed with an underscore (e.g. _spam) should be treated as a non-public part of the API (whether it is a function, a method or a data member). It should be considered an implementation detail and subject to change without notice.
Since there is a valid use-case for class-private members (namely to avoid name clashes of names with names defined by subclasses), there is limited support for such a mechanism, called name mangling. Any identifier of the form __spam (at least two leading underscores, at most one trailing underscore) is textually replaced with _classname__spam, where classname is the current class name with leading underscore(s) stripped. This mangling is done without regard to the syntactic position of the identifier, as long as it occurs within the definition of a class.
Python 中不存在只能从对象内部访问的“私有”实例变量。但是,大多数 Python 代码都遵循一个约定:带有下划线前缀的名称(例如 _spam)应该被视为 API 的非公开部分(无论是函数、方法还是数据成员) . 它应被视为实施细节,如有更改,恕不另行通知。
由于类私有成员有一个有效的用例(即避免名称与子类定义的名称的名称冲突),因此对这种称为名称修改的机制的支持有限。任何 __spam 形式的标识符(至少两个前导下划线,最多一个尾随下划线)在文本上替换为 _classname__spam,其中 classname 是当前类名,去掉了前导下划线。只要它出现在类的定义中,就可以不考虑标识符的句法位置来完成这种修改。
回答by Vishnu Upadhyay
static attributes are data attributesin python. so that attributes assigned in class:-
静态属性是python中的数据属性。以便在类中分配的属性:-
>>>class A(object):
>>> a = 1
>>>A.a
>>>1
This is different from C++ and Java, where a static member can't be accessed using an instance:-
这与 C++ 和 Java 不同,后者不能使用实例访问静态成员:-
>>>inst = A()
>>>inst.a
1
>>>
also builtin method setattr
will help you to set static variable(data attribute)
.
内置方法setattr
也将帮助您设置static variable(data attribute)
.
>>>setattr(A, 'b', 2)
>>>A.b
>>>inst.b
回答by Marcin
Just to add to it, you can have static variables in functions as well, not only classes:
只是为了添加它,您还可以在函数中使用静态变量,而不仅仅是类:
def some_fun():
some_fun.i += 1
print(some_fun.i)
some_fun.i = 0;
print(some_fun(), some_fun(), some_fun())
# prints: 1,2,3
回答by Sergeev Andrew
You can use standard @property decorator to make static attribute:
您可以使用标准的 @property 装饰器来制作静态属性:
class A(object):
@property
def a(self):
return 1
a = A()
print a.a
1
a.a = 2
AttributeError: can't set attribute