2

Python に問題があり、助けが必要です。関数を呼び出すと、出力が表示されなくなりました<function hello at 0x0000000002CD2198>(hello は関数名です)。Python を再インストールしましたが、問題は残ります。先日は晴れていて、一見理由もなく発生し始めました。

これを解決するにはどうすればよいですか?

4

4 に答える 4

8

関数を呼び出す必要があります。関数オブジェクト自体だけを出力しています。

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

helloと行の違いに注意してくださいhello()

于 2012-09-05T17:05:13.787 に答える
4

call function as 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>'
于 2012-09-05T17:05:34.373 に答える
2

私はあなたが電話helloしたと思います

hello

hello()代わりに試す

于 2012-09-05T17:05:31.313 に答える
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
于 2012-09-05T17:27:09.837 に答える