如何测试一个对象是否是另一个对象?

编程入门 行业动态 更新时间:2024-10-27 13:30:50
本文介绍了如何测试一个对象是否是另一个对象?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧! 问题描述

如果两个对象具有相同的值,则可以将它们与==进行比较。我想要做的就是找出两个对象实际上只是对同一个对象的引用,我怎么能用Python做到这一点? 谢谢

If two objects are of equal value you can compare them with ==. What I want to do is find out if two objects are actually just references to the same object, how can I do this in Python? Thanks

推荐答案

da ******* @ gmail écrit: 如果两个对象具有相同的值,您可以将它们与==进行比较。我想要做的是找出两个对象实际上是否只是对同一个对象的引用,我该如何在Python中执行此操作? If two objects are of equal value you can compare them with ==. What I want to do is find out if two objects are actually just references to the same object, how can I do this in Python?

最明显的方式(像往常一样?): 如果obj1是obj2: //你的代码在这里

The most obvious way (as usual ?): if obj1 is obj2: // your code here

对于删除我的消息感到抱歉,我发布了错误的谷歌 帐户,我真的不希望我的电子邮件在那些恼人的垃圾邮件机器人 可以找到它。 Sorry about removing my message, I posted with the wrong google account, I don''t really want my email where those irritating spam bots can find it. 最明显的方式(像往常一样?): 如果obj1是obj2: //你的代码在这里 我马上就想到了,并在控制台上对它进行了测试,但它没有像我预期的那样工作: foo = 3 bar = 3 zoo = foo foo是动物园 Truefoo是酒吧 Truezoo是酒吧 The most obvious way (as usual ?):if obj1 is obj2: // your code here I immediately thought of is, and tested it in the console, but it didn''t work quite like I expected: foo = 3bar = 3zoo = foofoo is zoo Truefoo is bar Truezoo is bar

True 很明显foo和bar有相同的价值,但它们是不同的对象 不是吗?然而应用is运算符产生True。 谢谢, -Dan

True clearly foo and bar have the same value but they are different objects aren''t they? Yet applying the is operator yields True. Thanks, -Dan

el******@yahoo 写道: foo = 3 bar = 3 foo = 3bar = 3

很明显foo和bar有相同的值,但它们是不同的对象不是吗?

clearly foo and bar have the same value but they are different objects aren''t they?

不,他们是同一个对象。现在尝试使用300而不是3 ;-)。

No, they''re the same object. Now try it with 300 instead of 3 ;-).

更多推荐

如何测试一个对象是否是另一个对象?

本文发布于:2023-11-22 09:55:11,感谢您对本站的认可!
本文链接:https://www.elefans.com/category/jswz/34/1616966.html
版权声明:本站内容均来自互联网,仅供演示用,请勿用于商业和其他非法用途。如果侵犯了您的权益请与我们联系,我们将在24小时内删除。
本文标签:对象   测试

发布评论

评论列表 (有 0 条评论)
草根站长

>www.elefans.com

编程频道|电子爱好者 - 技术资讯及电子产品介绍!