Compound assignment to Python class and instance variables

前端 未结 5 1957
天涯浪人
天涯浪人 2020-12-10 18:04

I\'ve been trying to understand Python\'s handling of class and instance variables. In particular, I found this answer quite helpful. Basically it says that if you declare a

相关标签:
5条回答
  • 2020-12-10 18:35

    Personally, I've found these documents by Shalabh Chaturvedi extremely useful and informative regarding this subject matter.

    bar.num += 1 is a shorthand for bar.num = bar.num + 1. This is picking up the class variable Foo.num on the righthand side and assigning it to an instance variable bar.num.

    0 讨论(0)
  • 2020-12-10 18:41
    bar.num += 1
    

    creates a new instance variable 'num' on the 'bar' instance because it doesn't yet exist (and then adds 1 to this value)

    an example:

    class Foo:
      def __init__(self):
        self.num= 1
    
    bar = Foo()
    print bar.num
    

    this prints 1

    print bar.foo
    

    this gives an error as expected: Traceback (most recent call last): File "", line 1, in AttributeError: Foo instance has no attribute 'foo'

    bar.foo = 5
    print bar.foo
    

    now this prints 5

    so what happens in your example: bar.num is resolved as Foo.num because there's only an class attribute. then foo.num is actually created because you assign a value to it.

    0 讨论(0)
  • 2020-12-10 18:44

    Searching for this very question, both this StackOverflow question and two (rather old, but valid) slides by Guido van Rossum (1, 2) came up high. Guido's slides state this behavior has to do with Python's search order for accessing the attribute (in the case of the example above num). Thought it'd be nice to put the two together right here :)

    0 讨论(0)
  • 2020-12-10 18:45

    In the following code, num is a class member.

    class Foo:
        num = 0
    

    A C++ equivalent would be something like

    struct Foo {
      static int num;
    };
    
    int Foo::num = 1;
    

    class Foo:
        def __init__(self):
            self.num = 0
    

    self.num is an instance member (self being an instance of Foo).

    In C++, it would be something like

    struct Foo {
      int num;
    };
    

    I believe that Python allows you to have a class member and an instance member sharing the same name (C++ doesn't). So when you do bar = Foo(), bar is an instance of Foo, so with bar.num += 1, you increment the instance member.

    0 讨论(0)
  • 2020-12-10 18:46

    I think you just found a bug in Python there. bar.num += 1 should be an error, instead, it is creating an attribute num in the object bar, distinct from Foo.num.

    It's a really strange behavior.

    0 讨论(0)
提交回复
热议问题