在x = 1中,x和1都是对象吗? [英] In x = 1, are both x and 1 objects?

查看:62
本文介绍了在x = 1中,x和1都是对象吗?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

x = 1 中, x 1 都是对象吗?因为 print(1) x = 1;print(x)将产生相同的输出.

In x = 1, are both x and 1 objects? Because print(1) and x = 1; print(x) will result in the same output.

即使 print 函数的语法是:

print( * objects ,sep ='',end ='\ n',file = sys.stdout,flush = False)

print(*objects, sep=' ', end='\n', file=sys.stdout, flush=False)

推荐答案

Python中的名称不是对象.在表达式中使用名称会自动将名称计算为对象所引用的.不能以任何方式与名称本身进行交互,例如传递名称或在名称上调用方法.

Names in Python are not objects. Using a name in an expression automatically evaluates to the object referred to by the name. It is not possible to interact with the name itself in any way, such as passing it around or calling a method on it.

>>> x = 1
>>> type(1)    # pass number to function...
<class 'int'>  # ...and receive the number!
>>> type(x)    # pass name to function...
<class 'int'>  # ...but receive the target!

请注意,从技术上讲, 1 也不是对象,而是对象的 literal .只有对象可以被传递-它不会透露它是来自文字 1 还是例如 2-1-之类的数学表达式.

Note that technically, 1 is also not an object but a literal of an object. Only the object can be passed around – it does not reveal whether it originates from a literal 1 or, for example, a mathematical expression such as 2 - 1.

这篇关于在x = 1中,x和1都是对象吗?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆