频道栏目
首页 > 程序开发 > Web开发 > Python > 正文
python static variable
2012-03-08 10:41:14           
收藏   我要投稿

Variables declared inside the class definition, but not inside a method are class or static variables:
>>> class MyClass:
...     i = 3
...
>>> MyClass.i

As @Daniel points out, this creates a class-level "i" variable, but this is distinct from any instance-level "i" variable, so you could have
>>> m = MyClass()
>>> m.i = 4
>>> MyClass.i, m.i
>>> (3, 4)
This is different from C++ and Java, but not so different from C#, where a static variable can't be accessed from an instance at all.
See what the Python tutorial has to say on the subject of classes and class objects.
@Steve Johnson has already answered regarding static methods, also documented under "Built-in Functions" in the Python Library Reference.
class C:
    @staticmethod
    def f(arg1, arg2, ...): ...
@beidy recommends classmethods over staticmethod, as the method then receives the class type as the first argument, but I'm still a little fuzzy on the advantages of this approach over staticmethod. If you are too, then it probably doesn't matter.
--------------------
 
@Blair Conrad said static variables declared inside the class definition, but not inside a method are class or "static" variables:
>>> class Test(object):
...     i = 3
...
>>> Test.i
3
There are a few gotcha's here. Carrying on from the example above:
>>> 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
Notice how the instance variable 't.i' got out of sync with the "static" class variable when the attribute 'i' was set directly on 't'. This is because 'i' was re-bound within the 't' namespace, which is distinct from the 'Test' namespace. If you want to change the value of a "static" variable, you must change it within the scope (or object) where it was originally defined. I put "static" in quotes because Python does not really have static variables in the sense that C++ and Java do.
Although it doesn't say anything specific about static variables or methods, the Python tutorial has some relevant information on classes and class objects.
@Steve Johnson also answered regarding static methods, also documented under "Built-in Functions" in the Python Library Reference.
class Test(object):
    @staticmethod
    def f(arg1, arg2, ...):
        ...
@beid also mentioned classmethod, which is similar to staticmethod. A classmethod's first argument is the class object. Example:
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 the the same as  Test.i = arg1

 

摘自  功夫 熊猫 
点击复制链接 与好友分享!回本站首页
相关TAG标签 python static variable
上一篇:socket端口重定向(python示例)
下一篇:python列表推导 和 内置函数map,filter 效率对比
相关文章
图文推荐
点击排行

关于我们 | 联系我们 | 广告服务 | 投资合作 | 版权申明 | 在线帮助 | 网站地图 | 作品发布 | Vip技术培训 | 举报中心

版权所有: 红黑联盟--致力于做实用的IT技术学习网站