I have this little test code to see how classes work.
class Person: def __init__(self): self.name = ("Mike") def myfunc(self): print("Hello my name is " + self.name) p1 = Person() hello = p1.myfunc() hello
What I want is for self.name to be returned from the class so I can use it outside of the class. Is this possible? I want the value to be returned without the 'hello my name is' string, so when printing the value returned the output is 'Mike' not 'Hello my name is Mike'. For now, it's just one value as this is a test code. If it works perfectly here, I will put it on my real code to make sure it works.
(1)Return the value(s) from the function in the class as normal, using return variable.
(2)Then to print it outside the class, variable = (myClass().variable) and then print variable.
(3) If you didn't want to print it out, just use the variable as normal.
You have successfully managed to get values from the class and used it in the whole program, without the need for the global keyword!
EDIT it looks like you don't even need to return the values: (myClass().variable) will get the value straight from the class without even needing to return the values, as supported by this code:
class Person: def __init__(self): self.name1 = ("Mike") self.name2 = ("John") self.name3 = ("Jane") self.name4 = ("Doe") name1, name2, name3, name4 = (Person().name1), (Person().name2), (Person().name3), (Person().name4) print (name1, name2, name3, name4)