Quantcast
Channel: What is the purpose of the `self` parameter? Why is it needed? - Stack Overflow
Viewing all articles
Browse latest Browse all 78

Answer by kame for What is the purpose of the word 'self', in Python?

$
0
0

I like this example:

class A: 
    foo = []
a, b = A(), A()
a.foo.append(5)
b.foo
ans: [5]

class A: 
    def __init__(self): 
        self.foo = []
a, b = A(), A()
a.foo.append(5)
b.foo
ans: []

Viewing all articles
Browse latest Browse all 78

Latest Images

Trending Articles



Latest Images

<script src="https://jsc.adskeeper.com/r/s/rssing.com.1596347.js" async> </script>