I have this python class with a method and a variable in it:
class Main:
def do_something(self):
#does something
x = 'something'
Is there a way to access the x variable outside of the class?
I have this python class with a method and a variable in it:
class Main:
def do_something(self):
#does something
x = 'something'
Is there a way to access the x variable outside of the class?
Yes there is, your existing code assigns 'something' to x, but this x is actually not associated with the class instance. Change x = 'something' to self.x = 'something' does the trick.
class Person:
def do_something(self):
self.x = 'something'
person = Person()
person.do_something()
print(person.x)
# Prints 'something'
Refer to this answer if you want to know more about the subtleties of initializing class variables.