数字被视为python中的对象吗? [英] Are numbers considered objects in python?

查看:73
本文介绍了数字被视为python中的对象吗?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我知道数值在 python 中是不可变的.我还阅读了 Python 中一切都是对象的方式.我只想知道数字类型是否也是python中的对象.因为如果它们是对象,那么变量实际上是引用变量,对吗?这是否意味着如果我将一个数字传递给一个函数并在一个函数内修改它,那么会创建两个具有两个引用的数字对象?python中有原始数据类型的概念吗?

注意:我也认为它是对象.但是在 python 导师中可视化说不同:http://www.pythontutor.com/visualize.html#mode=edit

def test(a):a+=10b=100测试(b)

或者是可视化工具的缺陷?

解决方案

是数字类型对象吗?

<预><代码>>>>isinstance(1, 对象)真的

显然是.:-).

请注意,您可能需要稍微调整一下对象的心智模型.在我看来,您认为 object 是可变的"——事实并非如此.实际上,我们需要将 Python 名称视为对对象的引用.该对象可能持有对其他对象的引用.

name = 某物

这里,右边被求值——所有的名字都被解析为对象,表达式的结果(一个对象)被name"引用.

好的,现在让我们考虑将某些内容传递给函数时会发生什么.

def foo(x):x = 2z = 3富(z)打印(z)

我们期望在这里发生什么?好吧,首先我们创建函数foo.接下来,我们创建对象 3 并通过名称 z 引用它.之后,我们查找 z 引用的值并将该值传递给 foo.输入 foo 后,该值将被(本地)名称 x 引用.然后我们创建对象 2 并通过本地名称 x 引用它.注意,x 与全局 z 无关——它们是独立的引用.仅仅因为它们在您输入函数时引用同一个对象并不意味着它们必须始终引用该函数.我们可以使用赋值语句随时更改名称所引用的内容.

注意,您的 += 示例似乎使事情复杂化,但您可以将 a += 10 视为 a = a + 10 如果它在这种情况下有帮助.有关 += 的更多信息,请查看:何时是";我+= x"不同于i = i + x"在 Python 中?

I am aware that numeric values are immutable in python. I have also read how everything is an object in python. I just want to know if numeric types are also objects in python. Because if they are objects, then the variables are actually reference variables right? Does it mean that if I pass a number to a function and modify it inside a function, then two number objects with two references are created? Is there a concept of primitive data types in python?

Note: I too was thinking it as objects. But visualizing in python tutor says differnt: http://www.pythontutor.com/visualize.html#mode=edit

def test(a):
    a+=10
b=100
test(b)

Or is it a defect in the visualization tool?

解决方案

Are numeric types objects?

>>> isinstance(1, object)
True

Apparently they are. :-).

Note that you might need to adjust your mental model of an object a little. It seems to me that you're thinking of object as something that is "mutable" -- that isn't the case. In reality, we need to think of python names as a reference to an object. That object may hold references to other objects.

name = something

Here, the right hand side is evaluated -- All the names are resolved into objects and the result of the expression (an object) is referenced by "name".

Ok, now lets consider what happens when you pass something to a function.

def foo(x):
   x = 2

z = 3
foo(z)
print(z)

What do we expect to happen here? Well, first we create the function foo. Next, we create the object 3 and reference it by the name z. After that, we look up the value that z references and pass that value to foo. Upon entering foo, that value gets referenced by the (local) name x. We then create the object 2 and reference it by the local name x. Note, x has nothing to do with the global z -- They're independent references. Just because they were referencing the same object when you enter the function doesn't mean that they have to reference the function for all time. We can change what a name references at any point by using an assignment statement.

Note, your example with += may seem to complicate things, but you can think of a += 10 as a = a + 10 if it helps in this context. For more information on += check out: When is "i += x" different from "i = i + x" in Python?

这篇关于数字被视为python中的对象吗?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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