Str 已定义为全局变量
Str is already defined as a global variable
我在使用函数时注意到一些非常奇怪的事情。看起来变量名 'str' 已经定义为全局变量。看一看:
def Example(x):
str = input()
return str
print (Example(str))
#When typing 'Hello!' Output --> Hello!
函数Example中定义了变量str。那么为什么没有 NameError: name 'str' is not defined 呢?
当我调用变量 x 或其他东西时(在本例中 'bar'):
def Example(x):
bar = input()
return bar
print (Example(bar))
#Output: NameError: name 'bar'is not defined
为什么名称为 'str' 的变量充当全局变量?
在python中,str()是字符串构造函数。它用于将对象转换为字符串。
您可以在本地使用它,但它会覆盖对函数的访问。您将无法再使用 str()。
供参考:
https://docs.python.org/2/library/functions.html#str
class str(object='')
Return a string containing a nicely printable representation of an
object. For strings, this returns the string itself. The difference
with repr(object) is that str(object) does not always attempt to
return a string that is acceptable to eval(); its goal is to return a
printable string. If no argument is given, returns the empty string,
''.
出于一般知识目的,如果删除变量,您可以取回构造函数。例如:
test = 1
str(test)
>>>'1'
str = 2
str(test)
>>>TypeError: 'int' object is not callable
del str
str(test)
>>>'1'
失败的原因:
def Example(x):
bar = input()
return bar
print (Example(bar))
#Output: NameError: name 'bar'is not defined
是因为您试图将变量 bar
传递给 Example()
方法,但 bar
在调用之前从未在任何地方定义。
我不太确定你想用这个方法完成什么,因为你传递了一个变量但根本没有使用它。
评论回复:
str
不是内置函数(尽管列在 page), but rather it is the constructor for the built-in type str
上)。为了表明您只是重新分配与关键字关联的方法(不一定是保留的,但它是尽管如此关键字),请考虑以下内容:
>>> str
<class 'str'>
>>> abs
<built-in function abs>
>>> str = abs
>>> str
<built-in function abs>
因此,您实际上已经覆盖了对 str
class 构造函数的赋值。我在这个例子中使用了 abs
,但同样适用于 input
:
>>> str
<class 'str'>
>>> input
<built-in function input>
>>> str = input
>>> str
<built-in function input>
>>> str = input()
hello world
>>> str
'hello world'
此处的不同之处在于,您为关键字 str
分配了一个字符串(str
类型)。所以你永远不能使用 str(10)
来获得 '10'
因为那现在就像调用 hello world(10)
失败了。
如果您想使用关键字作为变量名,按照惯例,尾部使用单个下划线以避免与 Python 关键字发生冲突,例如:
single_trailing_underscore_
我在使用函数时注意到一些非常奇怪的事情。看起来变量名 'str' 已经定义为全局变量。看一看:
def Example(x):
str = input()
return str
print (Example(str))
#When typing 'Hello!' Output --> Hello!
函数Example中定义了变量str。那么为什么没有 NameError: name 'str' is not defined 呢?
当我调用变量 x 或其他东西时(在本例中 'bar'):
def Example(x):
bar = input()
return bar
print (Example(bar))
#Output: NameError: name 'bar'is not defined
为什么名称为 'str' 的变量充当全局变量?
在python中,str()是字符串构造函数。它用于将对象转换为字符串。
您可以在本地使用它,但它会覆盖对函数的访问。您将无法再使用 str()。
供参考: https://docs.python.org/2/library/functions.html#str
class str(object='')
Return a string containing a nicely printable representation of an object. For strings, this returns the string itself. The difference with repr(object) is that str(object) does not always attempt to return a string that is acceptable to eval(); its goal is to return a printable string. If no argument is given, returns the empty string, ''.
出于一般知识目的,如果删除变量,您可以取回构造函数。例如:
test = 1
str(test)
>>>'1'
str = 2
str(test)
>>>TypeError: 'int' object is not callable
del str
str(test)
>>>'1'
失败的原因:
def Example(x):
bar = input()
return bar
print (Example(bar))
#Output: NameError: name 'bar'is not defined
是因为您试图将变量 bar
传递给 Example()
方法,但 bar
在调用之前从未在任何地方定义。
我不太确定你想用这个方法完成什么,因为你传递了一个变量但根本没有使用它。
评论回复:
str
不是内置函数(尽管列在 page), but rather it is the constructor for the built-in type str
上)。为了表明您只是重新分配与关键字关联的方法(不一定是保留的,但它是尽管如此关键字),请考虑以下内容:
>>> str
<class 'str'>
>>> abs
<built-in function abs>
>>> str = abs
>>> str
<built-in function abs>
因此,您实际上已经覆盖了对 str
class 构造函数的赋值。我在这个例子中使用了 abs
,但同样适用于 input
:
>>> str
<class 'str'>
>>> input
<built-in function input>
>>> str = input
>>> str
<built-in function input>
>>> str = input()
hello world
>>> str
'hello world'
此处的不同之处在于,您为关键字 str
分配了一个字符串(str
类型)。所以你永远不能使用 str(10)
来获得 '10'
因为那现在就像调用 hello world(10)
失败了。
如果您想使用关键字作为变量名,按照惯例,尾部使用单个下划线以避免与 Python 关键字发生冲突,例如:
single_trailing_underscore_