[英]Python proper use of __str__ and __repr__
My current project requires extensive use of bit fields. 我目前的项目需要广泛使用位字段。 I found a simple, functional recipe for bit a field class but it was lacking a few features I needed, so I decided to extend it.
我找到了一个简单的,功能性的配方,用于一个字段类,但它缺少我需要的一些功能,所以我决定扩展它。 I've just got to implementing
__str__
and __repr__
and I want to make sure I'm following convention. 我只需要实现
__str__
和__repr__
,我想确保我遵循惯例。
__str__
is supposed to be informal and concice, so I've made it return the bit field's decimal value (ie str(bit field 11)
would be "3"
. __str__
应该是非正式和简洁的,所以我让它返回位字段的十进制值(即str(bit field 11)
将是"3"
。
__repr__
is supposed to be a official representation of the object, so I've made it return the actual bit string (ie repr(bit field 11)
would be "11"
). __repr__
应该是对象的官方表示,所以我让它返回实际的位串(即repr(bit field 11)
将是"11"
)。
In your opinion would this implementation meet the conventions for str
and repr
? 在您看来,这个实现是否符合
str
和repr
的约定?
Additionally, I have used the bin()
function to get the bit string of the value stored in the class. 另外,我使用了
bin()
函数来获取存储在类中的值的位串。 This isn't compatible with Python < 2.6, is there an alternative method? 这与Python <2.6不兼容,有替代方法吗?
Cheers, 干杯,
Pete 皮特
The __repr__
should preferably be a string that could be used to recreate the object, for example if you use eval
on it - see the docs here . __repr__
应该最好是一个可以用来重新创建对象的字符串,例如,如果你在它上面使用eval
- 请参阅这里的文档。 This isn't an exact science, as it can depend on how the user of your module imported it, for example. 这不是一门精确的科学,因为它可能取决于模块用户如何导入它。
I would have the __str__
return the binary string, and the __repr__
return Classname(binary_string)
, or whatever could be used to recreate the object. 我希望
__str__
返回二进制字符串, __repr__
返回Classname(binary_string)
,或者可以用来重新创建对象的任何东西。
In the bitstring module (which I maintain) the __str__
is hexadecimal if the bitstring is a multiple of 4 bits long, otherwise it's either binary or a combination of the two. 在bitstring模块(我维护)中,如果bitstring是4位长的倍数,则
__str__
是十六进制的,否则它是二进制或两者的组合。 Also if the bitstring is very long then it gets truncated (you don't want to try to print a 100 MB bitstring in an interactive session!) 此外,如果bitstring非常长,那么它会被截断(你不想尝试在交互式会话中打印100 MB的bittring!)
I'd avoid using the bin()
function altogether if I were you. 如果我是你,我会完全避免使用
bin()
函数。 The reason being that it can't be used if your bitstring starts with zero bits (see my question here ). 原因是,如果你的比特串零位(见我的问题开始不能用在这里 )。 I'd advise using either use a
bin
method or property instead. 我建议使用
bin
方法或属性。
I'd consider having __str__
return a hexadecimal representation instead. 我会考虑让
__str__
返回一个十六进制表示。 This way, it's easier to eyeball what the actual bit values are, and thus more useful. 通过这种方式,可以更容易地观察实际比特值是什么,因此更有用。 But still quite concise (in fact, fewer characters than the decimal representation).
但仍然非常简洁(实际上,字符数少于十进制表示)。
__repr__
needs to return something that, if passed to your constructor, would create a new object that is an identical copy of the original one. __repr__
需要返回一些内容,如果传递给您的构造函数,则会创建一个与原始对象完全相同的新对象。
Yours returns '11' but if you passed '11' to your constructor you would not get the same bitfield as a result. 你的返回'11',但是如果你通过'11'到你的构造函数,你将得不到相同的位域。 So this
__repr__
is not ok. 所以
__repr__
不行。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.