2012-09-05 24 views
2

我遇到了python問題,需要一些幫助。當調用任何函數時,它不再顯示輸出,而是<function hello at 0x0000000002CD2198>(你好是函數名)。我已經重新安裝了Python,但問題仍然存在。那天很好,似乎沒有理由就開始發生了。調用函數顯示輸出<function hello at 0x0000000002CD2198>

我該如何解決這個問題?

回答

8

您需要通話你的功能,你只需打印功能對象本身:

>>> def hello(): 
... return "Hello World" 
... 
>>> print hello() 
Hello World 
>>> print hello 
<function hello at 0x1062ce7d0> 

注意的hellohello()線之間的差異。

2

我猜你通過

hello 

稱爲hello嘗試hello()代替

4

通話功能func(),函數調用括號在他們面前:

>>> def hello(): 
     print "goodbye" 

>>> hello() #use parenthesis after function name 
goodbye 

>>> hello   #you're doing this 
<function hello at 0x946572c> 

>>>hello.__str__() 
'<function hello at 0x946572c>' 
1

貪圖完整性:

即使hello實際上被調用,它當然可以是hello()只是返回另一個函數。

考慮一下:

def hello(): 
     """Returns a function to greet someone. 
     """ 
     def greet(name): 
      return "Hello %s" % name 
     # Notice we're not calling `greet`, so we're returning the actual 
     # function object, not its return value 
     return greet 

    greeting_func = hello() 

    print greeting_func 
    # <function greet at 0xb739c224> 
    msg = greeting_func("World") 

    print msg 
    # Hello World 
相關問題