Try to search your question here, if you can't find : Ask Any Question Now ?

class members also creates instance members in python

HomeCategory: stackoverflowclass members also creates instance members in python
bhawya asked 3 weeks ago

It seems that python class members also are “secretly” copied to instance members, I have never heard of this. Is this described somewhere?

Here is a small example I did to investigate this (python 3.6.7 ubuntu 18:04).

class A:
    a = 'hello'  # class member

    def __init__(self):
        print(self.a)    # a has also become a instance member
        self.a = 'Hi'    # change the instance member of a
        print(A.a)       # class member a unchanged


a = A()
1 Answers
Best Answer
Matthias answered 3 weeks ago
Your Answer

5 + 1 =

Popular Tags

WP Facebook Auto Publish Powered By : XYZScripts.com