Python static variable

Source: Internet
Author: 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 coshould 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 incluented 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 has es 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 abve:

>>> 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 incluented 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 

 

 

 

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

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.