简体   繁体   English

Python为什么将我的函数调用读取为变量调用?

[英]Why is Python reading my function call as a variable call?

Using Python 2.7.9, this is my code: 使用Python 2.7.9,这是我的代码:

    def secondaction():
        secondaction = raw_input(prompt)

        if secondaction == "walk through door" or secondaction == "go through door":
        print "Well done! You enter the Treasure Room..."
        treasure_room()

        else:
        print "If you don't go through that door you will never leave this cave."        
        secondaction()

firstact = raw_input(prompt)
global handclenched
def firstaction():
elif firstact == "use sword" and handclenched:
    print "You killed the hand giant! A door appears behind it. What will you do?"
    secondaction()

When Powershell takes me to the secondaction() function after I enter 'use sword' with 'handclenched' set to True , I enter yh as the raw_input() value and Powershell puts forward this error message: 当Powershell在将'handclenched'设置为True进入'使用剑'后带我进入secondaction()函数时,我输入yh作为raw_input()值,并且Powershell提出以下错误消息:

You killed the hand giant! A door appears behind it. What will you do?
> yh
If you don't go through that door you will never leave this cave.
Traceback (most recent call last):
  File "ex36game.py", line 168, in <module>
    right_room()
  File "ex36game.py", line 166, in right_room
    firstaction()
  File "ex36game.py", line 147, in firstaction
    firstaction()
  File "ex36game.py", line 153, in firstaction
    secondaction()
  File "ex36game.py", line 136, in secondaction
    secondaction()
TypeError: 'str' object is not callable

However when I change the code to: 但是,当我将代码更改为:

 def secondaction():
    second_action = raw_input(prompt)

    if second_action == "walk through door" or second_action == "go through door":
    print "Well done! You enter the Treasure Room..."
    treasure_room()

    else:
    print "If you don't go through that door you will never leave this cave."        
    secondaction()

It all works fine and I get no error message. 一切正常,我没有收到任何错误消息。

Why can't Python read the secondaction() as a function call instead of code that invokes/calls (are those the correct words?) the secondfunction variable that raw_input() was assigned to? 为什么Python不能将secondaction()作为函数调用而不是将调用/调用的代码(这些单词正确吗?)作为raw_input()分配给的secondfunction变量secondfunction

In Python, functions are objects, and a function name is just a variable that happens to hold a function. 在Python中,函数是对象,函数名称只是恰好包含函数的变量。 If you reassign that name, it isn't holding your function any more. 如果您重新分配该名称,它将不再具有您的功能。

Because you redeclaring the name secondaction = raw_input(prompt) in local scope. 因为您在本地范围内重新声明了名称secondaction = raw_input(prompt)

Take a look at python-scopes-and-namespaces 看看python-scopes-and-namespaces

Because you wrote: 因为您写道:

secondaction = raw_input(prompt)

sectionaction is now a string, and you can't call a string as if it were a function. 现在, sectionaction是一个字符串,您不能像对待函数一样调用它。 A name can't have two meanings at the same time, and the most recent assignment takes precedence, so you have lost the reference to your function. 名称不能同时具有两种含义,并且最新的赋值具有优先权,因此您丢失了对函数的引用。 Use a different name for one of them, as you have done in the code that works. 就像您在有效代码中所做的那样,对其中一个使用不同的名称。

Why can't Python read the 'secondaction()' as a function call instead of code that invokes/calls (are those the correct words?) the 'secondfunction' variable Python为什么不能将'secondaction()'读取为函数调用,而不是调用/调用的代码(这些单词正确吗?)作为'secondfunction'变量

In Python, functions are variables. 在Python中,函数变量。 That is to say, functions are first-class objects, which are assigned to variables by the def statement; 也就是说,函数是一类对象,它们由def语句分配给变量。 there is no separate namespace for callable functions vs assignable variables like some languages have. 像某些语言一样,可调用函数与可分配变量没有单独的命名空间。

This means that when you write secondaction = raw_input(prompt) you are making a local variable called secondaction inside the function. 这意味着当您编写secondaction = raw_input(prompt)您正在函数内部创建一个称为secondaction的局部变量。 Now when you write secondaction anywhere in that function body you are referring to the local variable; 现在,当您在该函数主体中的任何位置编写secondaction时,都将引用局部变量。 writing secondaction() with parentheses doesn't get you access to a separate function namespace, it just tries to call the value of the local variable that secondaction represents, and strings aren't callable so you get an error. 用括号编写secondaction()并不secondaction()您访问单独的函数名称空间,它只是尝试调用secondaction表示的局部变量的值,并且字符串不可调用,因此会出现错误。

It also means you can do things like: 这也意味着您可以执行以下操作:

def foo(x):
    return x+1

>>> bar= foo
>>> lookup= {'thing': bar}
>>> lookup['thing']
<function foo>
>>> lookup['thing'](1)
2

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM