如何创建与'str'具有相似属性的自定义类? [英] How can I create customized classes that have similar properties as'str'?

查看:52
本文介绍了如何创建与'str'具有相似属性的自定义类?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我的意思是,所有相互相等的类实例应该只需要将一个实例减少到一个实例,这意味着对于这个

类的实例而言a和b之间没有区别是a和= = b。


谢谢。

I mean, all the class instances that equal to each other should be
reduced into only one instance, which means for instances of this
class there''s no difference between a is b and a==b.

Thank you.

推荐答案

我发现我经常需要这样的课程有两个原因。
首先,它在内存中是有效的。其次,当两个实例比较等式时,只比较它们的指针。 (我认为

这就是Python比较'str'的方式。


11月24日下午6:31,李承芳< fanglich ... @ gmail.comwrote:
I find myself frequently in need of classes like this for two reasons.
First, it''s efficient in memory. Second, when two instances are
compared for equality only their pointers are compared. (I think
that''s how Python compares ''str''s.

On Nov 24, 6:31 pm, Licheng Fang <fanglich...@gmail.comwrote:

我的意思是,所有相互相等的类实例应该是

只减少到一个实例,这意味着对于这个

类的实例,a和b之间没有区别= = b。


谢谢。
I mean, all the class instances that equal to each other should be
reduced into only one instance, which means for instances of this
class there''s no difference between a is b and a==b.

Thank you.


Licheng Fang写道:
Licheng Fang wrote:

我的意思是,所有类实例彼此相等应该将

简化为一个实例,这意味着对于这个

类的实例,a是b和a == b。$ b $之间没有区别b
I mean, all the class instances that equal to each other should be
reduced into only one instance, which means for instances of this
class there''s no difference between a is b and a==b.



如果您只想要a == b为True,a is b为True,

会重载is_属性你的班级。就个人而言,我没有看到

的任何优势是。


问候,

Bj?


-

BOFH借口#352:

电缆长度不一样。

If you only want that if "a == b" is True also "a is b" is True,
overload the is_ attribute of your class. Personally, I don''t see
any advantage in this.

Regards,
Bj?rn

--
BOFH excuse #352:

The cables are not the same length.


Licheng Fang写道:
Licheng Fang wrote:

我发现自己经常需要这样的课程,因为两个原因是
。首先,它在记忆中是有效的。
I find myself frequently in need of classes like this for two
reasons. First, it''s efficient in memory.



您使用的是数百万个对象还是MB大小的对象?否则,

这不是争论。


BTW,如果你通过某些操作做出== b和
$会发生什么b $ b之后改变b所以必须创建另一个对象实例?

这个实例管理是一个非常大的运行时开销。

Are you using millions of objects, or MB size objects? Otherwise,
this is no argument.

BTW, what happens if you, by some operation, make a == b, and
afterwards change b so another object instance must be created?
This instance management is quite a runtime overhead.


其次,当比较两个实例的相等性,只比较它们的b / b $ b $指针。
Second, when two instances are compared for equality only their
pointers are compared.



我声明对象管理通常会比平等测试吃更多的性能

。除了你有大量相等的

对象。如果是后者,你应该重新考虑你的程序

设计。

I state that the object management will often eat more performance
than equality testing. Except you have a huge number of equal
objects. If the latter was the case you should rethink your program
design.


(我认为这就是Python比较''str' 's $。
(I think that''s how Python compares ''str''s.



一般不会。在CPython中,只创建非常短的字符串

一次。

Generally not. In CPython, just very short strings are created only
once.


>> a =""
b =""
a是b
>>a=" "
b=" "
a is b



True

True


>> a =""
b =""
a是b
>>a=" "
b=" "
a is b



False


问候,

Bj?rn


-

BOFH借口#430:


鼠标出现了奶酪错误

False

Regards,
Bj?rn

--
BOFH excuse #430:

Mouse has out-of-cheese-error


这篇关于如何创建与'str'具有相似属性的自定义类?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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