Konstantin
Konstantin

Reputation: 3159

Class property that contains a dictionary

Could you please explain, why v2 doesn't contain {"A": 1, "B": 2, "C": 3} and v3 does?

class MyClass:

    def foo(self):

        v1 = self.d.get('A')
        print(v1)
        # 1

        v2 = self.d.update({"C": 3})
        print(v2)
        # None

        v3 = self.d
        v3.update({"C": 3})
        print(v3)
        # {'A': 1, 'B': 2, 'C': 3}

    @property
    def d(self):
        return {"A": 1, "B": 2}

mc = MyClass()
mc.foo()

Upvotes: 1

Views: 44

Answers (1)

Alan Raso
Alan Raso

Reputation: 263

self.d.update({"C": 3}) is a void function. It returns a None value. In your case, v2 is being re-assigned to None.

Upvotes: 1

Related Questions