Python:具有相同名称的函数和变量

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/22161717/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-08-19 00:23:51  来源:igfitidea点击:

Python: function and variable with same name

pythonfunctionvariables

提问by dvsaraiva

My question is why I can't call the function again? Or, how to make that?

我的问题是为什么我不能再次调用该函数?或者,如何做到这一点?

Suppose I have this function:

假设我有这个功能:

def a(x, y, z):
 if x:
     return y
 else:
     return z

and I call it with:

我称之为:

print a(3>2, 4, 5)

I get 4.

我得到 4。

But imagine that I declare a variable with the same name that the function (by mistake):

但是想象一下,我声明了一个与函数同名的变量(错误地):

a=2

Now, if I try to do:

现在,如果我尝试这样做:

a=a(3>4, 4, 5)

or:

或者:

a(3>4, 4, 5)

I will get this error: "TypeError: 'int' object is not callable"

我会收到这个错误:“TypeError: ‘int’ object is not callable”

It's not possible to assign the variable 'a' to the function?

无法将变量“a”分配给函数?

采纳答案by óscar López

After you do this:

执行此操作后:

a = 2

ais no longer a function, it's just an integer(you reassigned it!). So naturally the interpreter will complain if you try to invoke it as if it were a function, because you're doing this:

a不再是一个函数,它只是一个整数(你重新分配了它!)。因此,如果您尝试像调用函数一样调用它,解释器自然会抱怨,因为您正在这样做:

2()
=> TypeError: 'int' object is not callable

Bottom line: you can't have two things simultaneouslywith the same name, be it a function, an integer, or any other object in Python. Just use a different name.

底线:你不能同时拥有两个同名的东西,无论是函数、整数还是 Python 中的任何其他对象。只需使用不同的名称。

回答by Aaron Hall

You're assigning the name a to a function definition, and then reassigning it to an integer.

您将名称 a 分配给函数定义,然后将其重新分配给一个整数。

It's syntactically correct, but it's not what you want.

它在语法上是正确的,但不是您想要的。

It's best to give functions semantic names that describe what you're doing with the arguments being passed to them, and to give variables semantic names that describe what object they're pointing to. If you do that, you'll have more readable code and you certainly won't make this mistake again.

最好为函数提供语义名称来描述您对传递给它们的参数所做的事情,并为变量提供语义名称来描述它们指向的对象。如果这样做,您将拥有更易读的代码,而且您肯定不会再犯这个错误。

回答by durga

namesin python are typically identifiersfor a specific type, more like naming a box which stores a variable/function/methodor any object in python. When you are reassigning, you are just renaming a box .

python 中的名称通常identifiers用于特定类型,更像是命名一个存储变量/函数/方法或 python 中任何对象的框。当您重新分配时,您只是重命名一个 box 。

You can find that out by doing the below.

您可以通过执行以下操作来了解这一点。

Initially ais assigned a value 9, at location 140515915925784. As soon as I use the same identifier for a function , anow refers to a box containing the address of that functionin 4512942512

最初在 location 处a分配了一个值。当我使用相同的标识符的功能,现在是指含有一个盒该函数的地址9140515915925784a4512942512

Reassigning ato 3again points ato refer to a different address.

重新分配a3再次指向a引用不同的地址。

>>> a = 9
>>> id(a)
140515915925784
>>> def a(x):
...     return x
... 
>>> id(a)
4512942512
>>> a
<function a at 0x10cfe09b0>
>>> 
>>> 
>>> 
>>> a = 3
>>> id(a)
140515915925928
>>> a
3
>>>