我如何在Python中创建类(即static)变量或方法?
>>> class MyClass:
... i = 3
...
>>> MyClass.i
3
i
变量,但它与任何实例级别的 i
变量是不同的,所以你可以有。>>> m = MyClass()
>>> m.i = 4
>>> MyClass.i, m.i
>>> (3, 4)
class C:
@staticmethod
def f(arg1, arg2, ...): ...
@beidy建议使用classmethod而不是staticmethod,因为该方法将类类型作为第一个参数接收。
@classmethod
相对于 @staticmethod
的优势在于,无论是父类还是子类调用该方法,你始终可以获取到类的名称。而静态方法则缺少这种信息,因此无法调用重写的方法。 - Sebconst.py
的文件来保存常量,例如PI = 3.14
,然后您可以在任何地方导入它。您只需要使用 from const import PI
即可。 - Giszmoi = 3
不是一个静态变量,它是一个类属性,并且由于它与实例级别的属性i
不同,所以它不像其他语言中的静态变量行为一样。请参见下面 millerdev's answer, Yann's answer, 和 my answer。 - Ricki
(静态变量)的副本会存在于内存中吗? - Saurabh Jain@Blair Conrad说,在类定义内部但不在方法内部声明的静态变量是类或“静态”变量:
>>> class Test(object):
... i = 3
...
>>> Test.i
3
这里有一些需要注意的地方。从上面的例子延伸:
>>> t = Test()
>>> t.i # "static" variable accessed via instance
3
>>> t.i = 5 # but if we assign to the instance ...
>>> Test.i # we have not changed the "static" variable
3
>>> t.i # we have overwritten Test.i on t by creating a new attribute t.i
5
>>> Test.i = 6 # to change the "static" variable we do it by assigning to the class
>>> t.i
5
>>> Test.i
6
>>> u = Test()
>>> u.i
6 # changes to t do not affect new instances of Test
# Namespaces are one honking great idea -- let's do more of those!
>>> Test.__dict__
{'i': 6, ...}
>>> t.__dict__
{'i': 5}
>>> u.__dict__
{}
class Test(object):
@staticmethod
def f(arg1, arg2, ...):
...
@beid提到了classmethod,它与staticmethod类似。classmethod的第一个参数是类对象。例如:
class Test(object):
i = 3 # class (or static) variable
@classmethod
def g(cls, arg):
# here we can use 'cls' instead of the class name (Test)
if arg > cls.i:
cls.i = arg # would be the same as Test.i = arg1
正如其他答案所指出的那样,可以使用内置的装饰器轻松实现静态方法和类方法:
class Test(object):
# regular instance method:
def my_method(self):
pass
# class method:
@classmethod
def my_class_method(cls):
pass
# static method:
@staticmethod
def my_static_method():
pass
像往常一样,my_method()
的第一个参数绑定到类实例对象。相比之下,my_class_method()
的第一个参数绑定到类对象本身(例如,在这种情况下,Test
)。对于my_static_method()
,没有任何参数被绑定,而拥有参数是可选的。
然而,实现“静态变量”(好吧,至少是可变的静态变量,如果这不是自相矛盾的话……)并不像直接了当。正如millerdev在他的答案中指出的,问题在于Python的类属性并非真正的“静态变量”。请看:
class Test(object):
i = 3 # This is a class attribute
x = Test()
x.i = 12 # Attempt to change the value of the class attribute using x instance
assert x.i == Test.i # ERROR
assert Test.i == 3 # Test.i was not affected
assert x.i == 12 # x.i is a different object than Test.i
这是因为行x.i = 12
添加了一个新的实例属性i
到x
,而不是改变Test
类i
属性的值。
可以通过将类属性转换为属性来部分地实现预期的静态变量行为,即在多个实例之间同步属性(但与类本身不同步;请参见下面的“警告”):
class Test(object):
_i = 3
@property
def i(self):
return type(self)._i
@i.setter
def i(self,val):
type(self)._i = val
## ALTERNATIVE IMPLEMENTATION - FUNCTIONALLY EQUIVALENT TO ABOVE ##
## (except with separate methods for getting and setting i) ##
class Test(object):
_i = 3
def get_i(self):
return type(self)._i
def set_i(self,val):
type(self)._i = val
i = property(get_i, set_i)
现在你可以做到:x1 = Test()
x2 = Test()
x1.i = 50
assert x2.i == x1.i # no error
assert x2.i == 50 # the property is synced
静态变量现在将始终在所有类实例之间保持同步。(注意:除非某个类实例决定定义自己的 _i
版本!但是,如果有人决定这样做,他们就会得到他们想要的东西,不是吗?)
请注意,从技术上讲, i
根本不是“静态变量”; 它是一个属性
,是一种特殊类型的描述符。但是, property
行为现在等效于在所有类实例之间同步的(可变)静态变量。
对于不可变的静态变量行为,请简单省略 property
setter:
class Test(object):
_i = 3
@property
def i(self):
return type(self)._i
## ALTERNATIVE IMPLEMENTATION - FUNCTIONALLY EQUIVALENT TO ABOVE ##
## (except with separate methods for getting i) ##
class Test(object):
_i = 3
def get_i(self):
return type(self)._i
i = property(get_i)
现在尝试设置实例i
属性会返回一个AttributeError
:
x = Test()
assert x.i == 3 # success
x.i = 12 # ERROR
请注意,上述方法仅适用于您的类的实例 - 当使用类本身时,它们将无法工作。例如:
x = Test()
assert x.i == Test.i # ERROR
# x.i and Test.i are two different objects:
type(Test.i) # class 'property'
type(x.i) # class 'int'
代码行assert Test.i == x.i
会导致错误,因为Test
和x
的属性i
是两个不同的对象。
许多人会发现这很令人惊讶。然而,事实并非如此。如果我们回到检查我们的Test
类定义(第二个版本)时,我们会注意到这一行:
i = property(get_i)
显然,Test
类的成员变量i
必须是一个property
对象,这是从property
函数返回的对象类型。
如果你觉得上面的内容令人困惑,很可能你还在从其他语言(如Java或c++)的角度思考。你应该去学习property
对象,Python属性返回顺序,描述符协议以及方法解析顺序(MRO)。
我在下面提供了以上“坑”的解决方案;但是我强烈建议您在充分理解为什么assert Test.i = x.i
会导致错误之前,不要尝试执行类似以下代码的操作。
Test.i == x.i
我只是出于信息目的介绍以下(Python 3)解决方案,我并不认为它是一个“好的解决方案”。我怀疑在Python中模拟其他语言的静态变量行为是否真正有必要。然而,无论它是否实际上有用,下面的内容都应该有助于进一步理解Python的工作方式。
更新:这个尝试非常糟糕;如果您坚持要执行类似此操作的代码(提示:请不要这样做;Python是一种非常优雅的语言,强行将其变成另一种语言是完全没有必要的),请改用Ethan Furman的回答中的代码。
使用元类模拟其他语言的静态变量行为
元类是一个类的类。Python中所有类的默认元类(即Python 2.3后的“新式”类)是type
。例如:
type(int) # class 'type'
type(str) # class 'type'
class Test(): pass
type(Test) # class 'type'
不过,你可以像这样定义自己的元类:
class MyMeta(type): pass
然后像这样将它应用到您自己的类中(仅适用于Python 3):
class MyClass(metaclass = MyMeta):
pass
type(MyClass) # class MyMeta
下面是我创建的一个元类,试图模拟其他语言中“静态变量”的行为。它基本上通过替换默认的getter、setter和deleter来工作,这些版本会检查请求的属性是否是“静态变量”。
“静态变量”的目录存储在StaticVarMeta.statics
属性中。所有属性请求最初都会尝试使用替代解析顺序来解决。我将其称为“静态解析顺序”,或简称“SRO”。这是通过在给定类(或其父类)的“静态变量”集合中寻找所请求的属性来完成的。如果属性不出现在“SRO”中,则该类将退回到默认属性get/set/delete行为(即“MRO”)。
from functools import wraps
class StaticVarsMeta(type):
'''A metaclass for creating classes that emulate the "static variable" behavior
of other languages. I do not advise actually using this for anything!!!
Behavior is intended to be similar to classes that use __slots__. However, "normal"
attributes and __statics___ can coexist (unlike with __slots__).
Example usage:
class MyBaseClass(metaclass = StaticVarsMeta):
__statics__ = {'a','b','c'}
i = 0 # regular attribute
a = 1 # static var defined (optional)
class MyParentClass(MyBaseClass):
__statics__ = {'d','e','f'}
j = 2 # regular attribute
d, e, f = 3, 4, 5 # Static vars
a, b, c = 6, 7, 8 # Static vars (inherited from MyBaseClass, defined/re-defined here)
class MyChildClass(MyParentClass):
__statics__ = {'a','b','c'}
j = 2 # regular attribute (redefines j from MyParentClass)
d, e, f = 9, 10, 11 # Static vars (inherited from MyParentClass, redefined here)
a, b, c = 12, 13, 14 # Static vars (overriding previous definition in MyParentClass here)'''
statics = {}
def __new__(mcls, name, bases, namespace):
# Get the class object
cls = super().__new__(mcls, name, bases, namespace)
# Establish the "statics resolution order"
cls.__sro__ = tuple(c for c in cls.__mro__ if isinstance(c,mcls))
# Replace class getter, setter, and deleter for instance attributes
cls.__getattribute__ = StaticVarsMeta.__inst_getattribute__(cls, cls.__getattribute__)
cls.__setattr__ = StaticVarsMeta.__inst_setattr__(cls, cls.__setattr__)
cls.__delattr__ = StaticVarsMeta.__inst_delattr__(cls, cls.__delattr__)
# Store the list of static variables for the class object
# This list is permanent and cannot be changed, similar to __slots__
try:
mcls.statics[cls] = getattr(cls,'__statics__')
except AttributeError:
mcls.statics[cls] = namespace['__statics__'] = set() # No static vars provided
# Check and make sure the statics var names are strings
if any(not isinstance(static,str) for static in mcls.statics[cls]):
typ = dict(zip((not isinstance(static,str) for static in mcls.statics[cls]), map(type,mcls.statics[cls])))[True].__name__
raise TypeError('__statics__ items must be strings, not {0}'.format(typ))
# Move any previously existing, not overridden statics to the static var parent class(es)
if len(cls.__sro__) > 1:
for attr,value in namespace.items():
if attr not in StaticVarsMeta.statics[cls] and attr != ['__statics__']:
for c in cls.__sro__[1:]:
if attr in StaticVarsMeta.statics[c]:
setattr(c,attr,value)
delattr(cls,attr)
return cls
def __inst_getattribute__(self, orig_getattribute):
'''Replaces the class __getattribute__'''
@wraps(orig_getattribute)
def wrapper(self, attr):
if StaticVarsMeta.is_static(type(self),attr):
return StaticVarsMeta.__getstatic__(type(self),attr)
else:
return orig_getattribute(self, attr)
return wrapper
def __inst_setattr__(self, orig_setattribute):
'''Replaces the class __setattr__'''
@wraps(orig_setattribute)
def wrapper(self, attr, value):
if StaticVarsMeta.is_static(type(self),attr):
StaticVarsMeta.__setstatic__(type(self),attr, value)
else:
orig_setattribute(self, attr, value)
return wrapper
def __inst_delattr__(self, orig_delattribute):
'''Replaces the class __delattr__'''
@wraps(orig_delattribute)
def wrapper(self, attr):
if StaticVarsMeta.is_static(type(self),attr):
StaticVarsMeta.__delstatic__(type(self),attr)
else:
orig_delattribute(self, attr)
return wrapper
def __getstatic__(cls,attr):
'''Static variable getter'''
for c in cls.__sro__:
if attr in StaticVarsMeta.statics[c]:
try:
return getattr(c,attr)
except AttributeError:
pass
raise AttributeError(cls.__name__ + " object has no attribute '{0}'".format(attr))
def __setstatic__(cls,attr,value):
'''Static variable setter'''
for c in cls.__sro__:
if attr in StaticVarsMeta.statics[c]:
setattr(c,attr,value)
break
def __delstatic__(cls,attr):
'''Static variable deleter'''
for c in cls.__sro__:
if attr in StaticVarsMeta.statics[c]:
try:
delattr(c,attr)
break
except AttributeError:
pass
raise AttributeError(cls.__name__ + " object has no attribute '{0}'".format(attr))
def __delattr__(cls,attr):
'''Prevent __sro__ attribute from deletion'''
if attr == '__sro__':
raise AttributeError('readonly attribute')
super().__delattr__(attr)
def is_static(cls,attr):
'''Returns True if an attribute is a static variable of any class in the __sro__'''
if any(attr in StaticVarsMeta.statics[c] for c in cls.__sro__):
return True
return False
你也可以动态地向类添加类变量
>>> class X:
... pass
...
>>> X.bar = 0
>>> x = X()
>>> x.bar
0
>>> x.foo
Traceback (most recent call last):
File "<interactive input>", line 1, in <module>
AttributeError: X instance has no attribute 'foo'
>>> X.foo = 1
>>> x.foo
1
类的实例可以改变类变量
class X:
l = []
def __init__(self):
self.l.append(1)
print X().l
print X().l
>python test.py
[1]
[1, 1]
个人而言,我需要静态方法时通常会使用classmethod。这主要是因为我可以将类作为参数获取。
class myObj(object):
def myMethod(cls)
...
myMethod = classmethod(myMethod)
或者使用装饰器
class myObj(object):
@classmethod
def myMethod(cls)
对于静态属性..是时候查一下Python定义了..变量总是可以更改的。它们分为可变和不可变两种类型..此外,还有类属性和实例属性..与Java&C ++中的静态属性没有什么真正的相似之处
在Pythonic的意义上为什么要使用静态方法,如果它与类没有任何关系!如果我是你,我要么使用classmethod,要么定义与类无关的方法。
需要注意的一点是静态属性和实例属性之间的区别,如下面的示例所示:
class my_cls:
my_prop = 0
#static property
print my_cls.my_prop #--> 0
#assign value to static property
my_cls.my_prop = 1
print my_cls.my_prop #--> 1
#access static property thru' instance
my_inst = my_cls()
print my_inst.my_prop #--> 1
#instance property is different from static property
#after being assigned a value
my_inst.my_prop = 2
print my_cls.my_prop #--> 1
print my_inst.my_prop #--> 2
这意味着在将值分配给实例属性之前,如果我们尝试通过实例访问属性,则将使用静态值。在Python类中声明的每个属性始终都有一个静态内存插槽。
Python中的静态方法称为classmethod。看一下下面的代码:
class MyClass:
def myInstanceMethod(self):
print 'output from an instance method'
@classmethod
def myStaticMethod(cls):
print 'output from a static method'
>>> MyClass.myInstanceMethod()
Traceback (most recent call last):
File "<stdin>", line 1, in <module>
TypeError: unbound method myInstanceMethod() must be called [...]
>>> MyClass.myStaticMethod()
output from a static method
>>> MyClass.myInstanceMethod(MyClass())
output from an instance method
@staticmethod
定义;@classmethod
则表示类方法(主要用作备用构造函数,但也可以像静态方法一样接收调用者所属类的引用)。 - ShadowRanger可以有静态
类变量,但可能不值得这样做。
这是一个Python 3的概念验证代码--如果任何细节不对,代码可以进行调整以符合你所说的静态变量
的含义:
class Static:
def __init__(self, value, doc=None):
self.deleted = False
self.value = value
self.__doc__ = doc
def __get__(self, inst, cls=None):
if self.deleted:
raise AttributeError('Attribute not set')
return self.value
def __set__(self, inst, value):
self.deleted = False
self.value = value
def __delete__(self, inst):
self.deleted = True
class StaticType(type):
def __delattr__(cls, name):
obj = cls.__dict__.get(name)
if isinstance(obj, Static):
obj.__delete__(name)
else:
super(StaticType, cls).__delattr__(name)
def __getattribute__(cls, *args):
obj = super(StaticType, cls).__getattribute__(*args)
if isinstance(obj, Static):
obj = obj.__get__(cls, cls.__class__)
return obj
def __setattr__(cls, name, val):
# check if object already exists
obj = cls.__dict__.get(name)
if isinstance(obj, Static):
obj.__set__(name, val)
else:
super(StaticType, cls).__setattr__(name, val)
并且正在使用:
class MyStatic(metaclass=StaticType):
"""
Testing static vars
"""
a = Static(9)
b = Static(12)
c = 3
class YourStatic(MyStatic):
d = Static('woo hoo')
e = Static('doo wop')
还有一些测试:
ms1 = MyStatic()
ms2 = MyStatic()
ms3 = MyStatic()
assert ms1.a == ms2.a == ms3.a == MyStatic.a
assert ms1.b == ms2.b == ms3.b == MyStatic.b
assert ms1.c == ms2.c == ms3.c == MyStatic.c
ms1.a = 77
assert ms1.a == ms2.a == ms3.a == MyStatic.a
ms2.b = 99
assert ms1.b == ms2.b == ms3.b == MyStatic.b
MyStatic.a = 101
assert ms1.a == ms2.a == ms3.a == MyStatic.a
MyStatic.b = 139
assert ms1.b == ms2.b == ms3.b == MyStatic.b
del MyStatic.b
for inst in (ms1, ms2, ms3):
try:
getattr(inst, 'b')
except AttributeError:
pass
else:
print('AttributeError not raised on %r' % attr)
ms1.c = 13
ms2.c = 17
ms3.c = 19
assert ms1.c == 13
assert ms2.c == 17
assert ms3.c == 19
MyStatic.c = 43
assert ms1.c == 13
assert ms2.c == 17
assert ms3.c == 19
ys1 = YourStatic()
ys2 = YourStatic()
ys3 = YourStatic()
MyStatic.b = 'burgler'
assert ys1.a == ys2.a == ys3.a == YourStatic.a == MyStatic.a
assert ys1.b == ys2.b == ys3.b == YourStatic.b == MyStatic.b
assert ys1.d == ys2.d == ys3.d == YourStatic.d
assert ys1.e == ys2.e == ys3.e == YourStatic.e
ys1.a = 'blah'
assert ys1.a == ys2.a == ys3.a == YourStatic.a == MyStatic.a
ys2.b = 'kelp'
assert ys1.b == ys2.b == ys3.b == YourStatic.b == MyStatic.b
ys1.d = 'fee'
assert ys1.d == ys2.d == ys3.d == YourStatic.d
ys2.e = 'fie'
assert ys1.e == ys2.e == ys3.e == YourStatic.e
MyStatic.a = 'aargh'
assert ys1.a == ys2.a == ys3.a == YourStatic.a == MyStatic.a
当在任何成员方法之外定义一些成员变量时,该变量可以是静态的或非静态的,这取决于变量的表达方式。
例如:
#!/usr/bin/python
class A:
var=1
def printvar(self):
print "self.var is %d" % self.var
print "A.var is %d" % A.var
a = A()
a.var = 2
a.printvar()
A.var = 3
a.printvar()
结果是
self.var is 2
A.var is 1
self.var is 2
A.var is 3
@dataclass定义提供了用于定义实例变量和初始化方法__init__()
的类级名称。如果你想在@dataclass
中使用类级别变量,你应该使用typing.ClassVar
类型提示。 ClassVar
类型的参数定义了类级别变量的类型。
from typing import ClassVar
from dataclasses import dataclass
@dataclass
class Test:
i: ClassVar[int] = 10
x: int
y: int
def __repr__(self):
return f"Test({self.x=}, {self.y=}, {Test.i=})"
使用示例:
> test1 = Test(5, 6)
> test2 = Test(10, 11)
> test1
Test(self.x=5, self.y=6, Test.i=10)
> test2
Test(self.x=10, self.y=11, Test.i=10)
static
” 实际上有几个含义(由于评论长度非常严格,以下是缩写的定义)。有来自 C 的文件作用域static
,表示“此变量或函数仅可在此文件中使用”。还有类作用域static
,表示“此方法或字段与类型相关,而不是任何类型的实例”(在 C++ 中很少用,但在 C#、Java 和 ObjC 中很常见,例如,我认为这就是 OP 所问的内容)。还有函数中的局部变量static
,表示“此变量的值在函数调用之间保持不变”。 - jrh