2012-01-04 40 views
2

假設我的類methodA和methodB有兩個方法。在methodA中,我創建了一個類X的對象S,然後我想將這個對象傳遞給methodB。那麼,當我編寫methodB時,我想知道X的類型,這樣當我使用它時,輸入我的IDE,就可以輸入「X」。並看到S的方法和實例變量的列表。這在python中是不可能的嗎?Python:爲用戶定義對象完成代碼的編輯器

def methodA(self): 

    S = X() 
    self.methodB(S) 

def methodB(self, S): 

    "S."....#here I want to see all the methods of variable S. 

回答

3

這實際上並不是一個關於Python的問題。相反,這是一個關於IDE的問題。

我還沒有看到一個IDE能夠做你在問什麼。另外,鑑於Python的動態特性,除了一些特殊情況外,實現這種代碼完成將非常困難。

+0

+1是的,這是幾乎不可能知道python對象成員沒有運行代碼 – soulcheck 2012-01-04 15:16:07

0

如果您的IDE支持rope插件,您可以部分自動完成支持。

我使用(SublimeText)編輯器支持繩子插件,那麼我就可以鍵入:

class Class(object): 
    def __init__(self): 
     self.var1 = "one" 
     self.var2 = "two" 
     self.var3 = "three" 

    def method_1(self): 
     pass 

    def method_2(self): 
     self.var1 = "one**"  # Autocomplete works here, no need of workarounds 


def test_method(obj1_, obj2_): 
    # To make autocompletion works here, I start suffixing the parameters with _ 

    if False:    # Then I define each parameter statically 
     obj1 = Class() 
     obj2 = Class() 

    # Autocomplete now works! 
    obj1.method_1() 
    print obj2.var1 

    # Remember to rename obj1_ to obj1, ... 

### 

也許繩支持一種註釋,使上面的解決方法是不必要的,但我無法找到它的好例子。

相關問題