2014-06-18 329 views
0

如何從另一個模塊/應用程序(在Django中)調用靜態方法? 比如我聲明如下靜態方法調用靜態方法

class SomeClass (object): 
    @staticmethod 
    def SomeStaticMethod (firstArg, **kwargs): 
     # do something 

,並在另一個類我想使用它像這樣

SomeClass.SomeStaticMethod ('first', second=2, third='three', fourth=4) 

我試圖導入,但得到一個NameError:全局名稱「SomeClass的」被沒有定義

import myapp.SomeClass 

回答

2
>>> from somefile import SomeClass 
>>> SomeClass.SomeStaticMethod('first', second=2, third='three') 
first {'second': 2, 'third': 'three'} 

這也是很好的瞭解,靜態方法是完滿成功在大多數情況下是無用的,因爲模塊本身可以用作函數的命名空間。因此:

def SomeStaticFunction(a, **kwargs): 
    # do something 

和:

>>> import somefile 
>>> somefile.SomeStaticFunction(1, second=2, third='three') 
1 {'second': 2, 'third': 'three'} 
+0

感謝您的信息! – user2994682