ext
is not a global, it is temporarily a local when the class is being created. From the class
statement documentation:
The class’s suite is then executed in a new execution frame (see Naming and binding), using a newly created local namespace and the original global namespace. (Usually, the suite contains mostly function definitions.) When the class’s suite finishes execution, its execution frame is discarded but its local namespace is saved.
Emphasis mine. The local namespace then forms the class attributes.
By the time your MP3File.play()
method is being called, that local namespace is long since gone; you cannot just refer to ext
as if it still exists. It is now a class attribute instead!
You can address that attribute via the self
reference to an instance. Unless the instance also has an ext
attribute, the class attribute is found and returned instead:
def play(self):
print(self.ext)
or you can find it on the class:
def play(self):
print(MP3File.ext)