So I've got something that looks like this
class MyClass():
def someMethod(self):
sprites = SomeClassInstance()
class MyClass2():
def someMethod2(self):
#The answer to the question goes here
Is there a way to access sprites from MyClass2()? And to top it off there is no statement like this
A = MyClass()
there is only this
A = MyClass().someMethod()
Your sprites variable is local to MyClass.someMethod
. Instead, attach it to self
to make it accessible from outside the method:
class MyClass():
def someMethod(self):
self.sprites = SomeClassInstance()
class MyClass2():
def someMethod2(self):
#The answer to the question goes here
myClass = myClass()
myClass.someMethod()
sprites = myClass.sprites
If you want sprites to be a static variable, define it in the class body:
class MyClass():
sprites = SomeClassInstance()
def someMethod(self):
pass
class MyClass2():
def someMethod2(self):
#The answer to the question goes here
MyClass.sprites
This is quite hacky... but if you just want to get stuff done your way, you can use a global variable:
SPRITES = None
class A(object):
def setter(self):
global SPRITES
SPRITES = "mysprite"
class B(object):
def getter(self):
global SPRITES
print SPRITES
a, b = A(), B()
a.setter()
b.getter()
But of course all instances of A and B will share one variable so this might not be what you want.
The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.