Nov-16-2020, 07:56 AM
(This post was last modified: Nov-16-2020, 07:57 AM by Gribouillis.)
vizier87 Wrote:Though I wonder why it works if it's out of the class?That's the way scopes work in Python, or rather namespaces. Inside a method, the names that are defined in the class are not directly available.
foo = 1 # name in the **global** namespace class Spam: bar = 2 # in the namespace of the class body def method(self): qux = 3 # in the method's **local** namespace print(qux) # works. We see the local namespace print(foo) # also works. We see the global namespace print(bar) # doesn't work. We don't see the class' namespace print(self.bar) # works. We can access the class' members through the instance self.