我正在使用 Pygame2 多媒体库用 Python 编写游戏,但我更习惯于使用 ActionScript 3 开发游戏。在 AS3 中,我认为不可能将对象存储在静态变量中,因为静态变量在对象可以被实例化之前被初始化。
但是,在 Python 中,我不确定这是否成立。我可以将对象实例存储在 Python 类变量中吗?什么时候实例化?每个类或每个实例都会实例化一个吗?
class Test:
counter = Counter() # A class variable counts its instantiations
def __init__(self):
counter.count() # A method that prints the number of instances of Counter
test1 = Test() # Prints 1
test2 = Test() # Prints 1? 2?