0
我想通過使實例Outer(變量)來指定變量,而不是所有靜態類中使用的變量,我應該怎麼做?有沒有其他解決方案,而不是使用靜態方法,並將Outer傳遞給每個內部類?Python:如何從內部靜態類獲取外部類變量?
class Outer():
def __init__(self, variable):
self.variable= variable
class Inner1():
@staticmethod
def work1():
**print Outer.variable**
class Inner2():
@staticmethod
def work2():
**print Outer.variable**
見鬼,爲什麼你想在Python內部類,手工製作的靜態類?沒有優勢。 Python不是Java(也不是C#)。此外,您似乎將實例變量('some_obj.attr')與類(「static」)變量('SomeClass.attr')混合在一起。 – delnan
[Guido van Rossum on static methods:](http://groups.google.com/group/python-ideas/browse_thread/thread/6d82fd23609eff9c/eabf958d7d5f8382?lnk=gst&q=staticmethod+Guido+van+Rossum#eabf958d7d5f8382)「他們基本上是一個意外 - 在我發明新風格類和 描述符的Python 2.2版本中返回 ,我的意思是實現類方法,但起初我沒有理解它們,並且首先意外地實現了靜態方法。 然後刪除它們並且只提供類方法已經太晚了。「 –
有一個非常類似的問題[這裏]的答案(http://stackoverflow.com/questions/6665196/how-can-i-acess-a-metod-from-an-object-inside-a-class- in-python-2-6/6665452#6665452) – brandizzi