可能重复:
Python"is"运算符使用整数意外运行
通常我会用它type(x) == type(y)来比较类型是否相同.然后x==y用来比较数值是否相等.
然而,有人建议人们可以仅用于z1 is z2比较z1和z2是否包含具有完全相同值的相同类型的数字.在许多情况下,它会成功(特别是对于积极的整体).
但是,有时相同的数字(主要是负数)可以有几个不同的实例.这是python的预期行为吗?
例如:
>>> for x in range(-20,125):
z1=x
z2=int(float(x))
if z1 is not z2:
print "z1({z1}; type = {typez1}; id={idz1}) is not z2({z2}; type = {typez2}; id={idz2})".format(z1=z1,typez1=type(z1),idz1=id(z1),z2=z2,typez2=type(z2),idz2=id(z2))
z1(-20; type = <type 'int'>; id=33869592) is not z2(-20; type = <type 'int'>; id=33870384)
z1(-19; type = <type 'int'>; id=33870480) is not z2(-19; type = <type 'int'>; id=33870408)
z1(-18; type = <type 'int'>; id=32981032) is …Run Code Online (Sandbox Code Playgroud) 我想向某人展示如何使用is而不是==比较整数会失败。我认为这会起作用,但它没有:
>>> import copy
>>> x = 1
>>> y = copy.deepcopy(x)
>>> x is y
True
Run Code Online (Sandbox Code Playgroud)
对于更大的整数,我可以很容易地做到这一点:
>>> x = 500
>>> y = 500
>>> x is y
False
Run Code Online (Sandbox Code Playgroud)
我如何用较小的整数来演示相同的事情,这些整数通常可能用于 python 中的类似枚举的目的?
我开始阅读python的+ =语法并偶然发现以下帖子/答案: 关于+ =的交互式代码
所以我注意到框架和物体之间似乎存在差异.
在全局框架中,即使它们是不同的变量,它们也指向同一个对象; 如果这条线
l2 += [item]
Run Code Online (Sandbox Code Playgroud)
相反
l2 = l2 + [item]
Run Code Online (Sandbox Code Playgroud)
然后'l2'在该行运行时成为一个单独的对象.我最大的问题是你何时想要一个变量指向一个单独的对象?另外,为什么以及何时要让它们指向同一个对象?
任何解释或用例将不胜感激!如果您能提及与数据科学相关的任何内容,那就另外了
我正在使用repl.itpython3 - 这是我所做的一系列作业
x34=20
x34
=> 20
hex(id(x34))
=> '0x7fdb5d318960'
x34=30
hex(id(x34))
=> '0x7fdb5d318aa0'
hex(id(x34))
=> '0x7fdb5d318aa0'
x34
=> 30
hex(id(x34))
=> '0x7fdb5d318aa0'
x34=20
hex(id(x34))
=> '0x7fdb5d318960'
Run Code Online (Sandbox Code Playgroud)
为什么x34当我重新赋值时变量的地址会改变30?
我在Python 3.6.5中遇到了一个奇怪的现象:虽然它们彼此相等但float('50.0')不一样.float(50.0)
我运行了一些代码来找到差异.除了python说他们不一样,我找不到区别.我很困惑.如果有人能解释这里发生的事情,我会喜欢它.
这是我的测试:
if float('50.0') is float(50.0):
print("float('50.0') is float(50.0)")
else:
print("float('50.0') is not float(50.0)")
if float('50.0') == float(50.0):
print("float('50.0') == float(50.0)")
else:
print("float('50.0') != float(50.0)")
if float('50.0') is 50.0:
print("float('50.0') is 50.0")
else:
print("float('50.0') is not 50.0")
if float(50.0) is 50.0:
print('float(50.0) is 50.0')
else:
print('float(50.0) is not 50.0')
if float(50.0) is float(50.0):
print('float(50.0) is float(50.0)')
else:
print('float(50.0) is not float(50.0)')
xstr_float = float('50.0')
norm_float = float(50.0)
print ('xstr_float: {0:.100f}'.format(xstr_float))
print ('xstr_float is …Run Code Online (Sandbox Code Playgroud) 我试图在这里复制内存使用测试.
从本质上讲,帖子声称给出了以下代码片段:
import copy
import memory_profiler
@profile
def function():
x = list(range(1000000)) # allocate a big list
y = copy.deepcopy(x)
del x
return y
if __name__ == "__main__":
function()
Run Code Online (Sandbox Code Playgroud)
调用
python -m memory_profiler memory-profile-me.py
Run Code Online (Sandbox Code Playgroud)
在64位计算机上打印
Filename: memory-profile-me.py
Line # Mem usage Increment Line Contents
================================================
4 @profile
5 9.11 MB 0.00 MB def function():
6 40.05 MB 30.94 MB x = list(range(1000000)) # allocate a big list
7 89.73 MB 49.68 MB y = copy.deepcopy(x)
8 82.10 MB …Run Code Online (Sandbox Code Playgroud) 我接受了 Python 测试,其中一个问题是:应该将什么传递给以下函数,以便它返回True?
def fun(x):
if x + 1 is 1 + x:
return False
if x + 2 is not 2 + x:
return False
return True
Run Code Online (Sandbox Code Playgroud)
在我看来,这没有多大意义,但我只想知道正确的答案(如果存在这样的答案)。
对于Python中的某些类型,is运算符似乎等同于==运算符.例如:
>>> 1 is 1
True
>>> "a spoon" is "a spoon"
True
>>> (1 == 1) is (2 == 2)
True
Run Code Online (Sandbox Code Playgroud)
然而,这并非总是如此:
>>> [] == []
True
>>> [] is []
False
Run Code Online (Sandbox Code Playgroud)
这对于诸如列表之类的可变类型是有意义的.但是,诸如元组之类的不可变类型似乎显示相同的行为:
>>> (1, 2) == (1, 2)
True
>>> (1, 2) is (1, 2)
False
Run Code Online (Sandbox Code Playgroud)
这提出了几个问题:
==/ is相关不变性等价? 更新:如果总是通过引用分配,为什么不打印以下内容2?:
>>> a = 1
>>> b = a
>>> a = 2
>>> b
1 …Run Code Online (Sandbox Code Playgroud) 在Python中是否可以创建具有相同值的两个不可变对象?
所以你明白我的意思,这里有一些例子:
>>> a = 13
>>> b = 13
>>> a is b
True
>>> a = 13
>>> b = 26/2
>>> a is b
True
>>> a = 13
>>> b = int.__new__(int, 13)
>>> a is b
True
>>> a = 13
>>> b = int("13")
>>> a is b
True
Run Code Online (Sandbox Code Playgroud)
是否可以创建a并b具有相同的值但a is b返回False?刚刚学习......:D
我希望打印的值是"200!" 和"404!" 在Python3.4中.但我得到的结果是"200!" 和"别的!".
我做了一个奇怪的面孔 - 并在C中再次尝试.C中的结果与Python3中的结果不同.为什么?
Python3.4代码
def chk(s):
if s is 200:
print('200!')
elif s is 404:
print('404!')
else:
print('else!')
chk(200)
chk(404)
Run Code Online (Sandbox Code Playgroud)
Python3.4代码结果
200!
else!
Run Code Online (Sandbox Code Playgroud)
C代码
#include <stdio.h>
void chk(int s) {
if (s == 200)
printf("200!");
else if (s == 404)
printf("404!");
else
printf("else!");
}
int main(void) {
chk(200);
chk(404);
return 0;
}
Run Code Online (Sandbox Code Playgroud)
C代码结果
200!404!
Run Code Online (Sandbox Code Playgroud) python ×9
immutability ×2
c ×1
cpython ×1
dataframe ×1
identity ×1
memory ×1
numbers ×1
object ×1
python-3.4 ×1
python-3.x ×1
reference ×1