我的代码需要有一个内部类,我想创建这个内部类的实例而不创建外部类的实例。
如何在 python 中做到这一点? 在 java 中,我们可以将内部类定义为静态的,但我不知道如何在 python 中将内部类设为静态。我知道对于方法我们可以使用 @staticmethod
装饰器。
class Outer:
def __init__(self):
print 'Instance of outer class is created'
class Inner:
def __init__(self):
print 'Instance of Inner class is created'
最佳答案
类 Inner 是在类 Outer 的定义过程中定义的,之后它存在于它的类命名空间中。所以只是 Outer.Inner()
。
关于python - python中的静态内部类,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/17906817/