python static variable

來源:互聯網
上載者:User

Variables declared inside the class definition, but not inside a method are class or static variables:

>>> class MyClass: 
...     i = 3 
... 
>>> MyClass.i 
3  

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 

There are a few gotcha's here. Carrying on from the example above:

>>> t = Test() 
>>> t.i     # static variable accessed via instance 

>>> t.i = 5 # but if we assign to the instance ... 
>>> Test.i  # we have not changed the static variable 

>>> t.i     # we have overwritten Test.i on t by creating a new attribute t.i 

>>> Test.i = 6 # to change the static variable we do it by assigning to the class 
>>> t.i 

>>> Test.i 

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 

 

 

 

相關文章

聯繫我們

該頁面正文內容均來源於網絡整理,並不代表阿里雲官方的觀點,該頁面所提到的產品和服務也與阿里云無關,如果該頁面內容對您造成了困擾,歡迎寫郵件給我們,收到郵件我們將在5個工作日內處理。

如果您發現本社區中有涉嫌抄襲的內容,歡迎發送郵件至: info-contact@alibabacloud.com 進行舉報並提供相關證據,工作人員會在 5 個工作天內聯絡您,一經查實,本站將立刻刪除涉嫌侵權內容。

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.