Suppose I have the following code
str = "Hello World"
a = foo()
You are confused because you picked the wrong variable name.
str is the built-in string type. You have to use
self. prefix within your class methods and instance prefix outside them.
class foo: s = "Hello World" def test(self): print(s) # s not found print(self.s) # OK!
Note that you defined a class variable, shared between instances of the same class (with wierd effects sometimes). Reserve that usage to constants.
To define an instance variable do:
class foo: def __init__(self): self.s = "Hello World"
So you can change
s on instance
A without risking to change it on instance
B (well it's different for immutables like strings or ints, but you don't want to do that anyway)