python脚本和python空闲中的行为不同? [英] Different behavior in python script and python idle?

查看:57
本文介绍了python脚本和python空闲中的行为不同?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

在python空闲中:

In the python idle:

>>> a=1.1
>>> b=1.1
>>> a is b
False

但是当我将代码放入脚本并运行时,我会得到不同的结果:

But when I put the code in a script and run it, I will get a different result:

$cat t.py
a=1.1
b=1.1
print a is b
$python t.py
True

为什么这发生了吗?我知道比较两个对象的 id ,所以为什么两个对象的id在python中是相同/唯一的脚本/空闲吗?

Why did this happen? I know that is compares the id of two objects, so why the ids of two objects are same/unique in python script/idle?

我还发现,如果我使用小整数,例如 1 ,而不是 1.1 ,结果在python脚本和python idle中都是相同的。为什么小整数和小浮点数会有不同的行为?

I also found that, if I use a small int, for example 1, instead of 1.1, the result will be the same in both the python script and python idle. Why did small int and small float have different behavior?

我正在使用CPython 2.7.5。

I am using CPython 2.7.5.

推荐答案

Python执行脚本文件时,将首先解析整个文件。您会注意到,当您在某个地方引入语法错误时:无论它在哪里,它都会阻止任何行的执行。

When Python executes a script file, the whole file is parsed first. You can notice that when you introduce a syntax error somewhere: Regardless of where it is, it will prevent any line from executing.

因此,由于Python首先解析文件,因此立即数可以有效地加载到内存中。由于Python知道它们是常量,因此表示这些常量值的所有变量都可以指向内存中的同一对象。因此,对象是共享的。

So since Python parses the file first, literals can be loaded effectively into the memory. Since Python knows that these are constant, all variables that represent those constant values can point to the same object in memory. So the object is shared.

这适用于int和float,甚至适用于字符串;即使有一个常量表达式需要首先求值:

This works for ints and floats, but even for strings; even when there is a constant expression that needs to be evaluated first:

a = "foo"
b = "foo"
c = "fo" + "o"
print(a is b)
print(a is c)

现在在IDLE中,行为非常不同:作为交互式解释器,IDLE分别执行每一行。因此 a = 1.1 b = 1.1 是在单独的上下文中执行的,这使得(或者非常困难)找出它们共享相同的常量常量值并可以共享内存。因此,解释器将分配两个不同的对象,这将导致使用 is 的身份检查失败。

Now in IDLE, the behavior is very different: As an interactive interpreter, IDLE executes every line separately. So a = 1.1 and b = 1.1 are executed in separated contexts which makes it impossible (or just very hard) to figure out that they both share the same constant literal value and could share the memory. So instead, the interpreter will allocate two different objects, which causes the identity check using is to fail.

小整数,情况有些不同。因为它们经常使用,所以CPython静态地存储一组整数(范围在-5到256之间),并使这些值的每个值都指向相同的 int 对象。这就是为什么小整数得到的结果与其他任何对象都不同的原因。另请参阅以下问题:

For small integers, the situation is a bit different. Because they are often used, CPython stores a set of integers (in the range between -5 and 256) statically and makes that every value of these points to the same int object. That’s why you get a different result for small integers than for any other object. See also the following questions:

  • "is" operator behaves unexpectedly with integers
  • What's with the Integer Cache inside Python?

这篇关于python脚本和python空闲中的行为不同?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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