Python 正确使用 __str__ 和 __repr__

发布于 2024-08-31 23:36:19 字数 619 浏览 1 评论 0原文

我当前的项目需要大量使用位字段。我找到了一个简单、实用的位字段类的配方但它缺少一些我需要的功能,所以我决定扩展它。我刚刚需要实现 __str____repr__ 并且我想确保遵循约定。

__str__ 应该是非正式且简洁的,所以我让它返回位字段的十进制值(即 str(bit field 11) 将是 "3 “.

__repr__ 应该是该对象的官方表示,因此我让它返回实际的位字符串(即 repr(bit field 11) 将是 "11")。

您认为此实现是否符合 strrepr 的约定?

另外,我使用了 < 在类中的值的位字符串,这与 Python <2.6 不兼容,有替代方法吗

code>bin() 函数来获取存储

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__ is supposed to be informal and concice, so I've made it return the bit field's decimal value (i.e. str(bit field 11) would be "3".

__repr__ is supposed to be a official representation of the object, so I've made it return the actual bit string (i.e. repr(bit field 11) would be "11").

In your opinion would this implementation meet the conventions for str and repr?

Additionally, I have used the bin() function to get the bit string of the value stored in the class. This isn't compatible with Python < 2.6, is there an alternative method?

Cheers,

Pete

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(3

笔芯 2024-09-07 23:36:19

__repr__ 最好是一个可用于重新创建对象的字符串,例如,如果您对其使用 eval - 请参阅文档 此处。这不是一门精确的科学,因为它可能取决于模块的用户如何导入它。

我会让 __str__ 返回二进制字符串,而 __repr__ 返回 Classname(binary_string) 或任何可用于重新创建对象的内容。

bitstring 模块(我维护的)中,__str__ 是十六进制,如果bitstring 是 4 位长的倍数,否则它是二进制或两者的组合。另外,如果位串非常长,那么它会被截断(您不想尝试在交互式会话中打印 100 MB 的位串!)

如果满足以下条件,我会完全避免使用 bin() 函数:我就是你。原因是,如果您的位字符串以零位开头,则无法使用它(请参阅我的问题 此处)。我建议使用 bin 方法或属性来代替。

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. 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.

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. 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!)

I'd avoid using the bin() function altogether if I were you. 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.

晌融 2024-09-07 23:36:19

我会考虑让 __str__ 返回十六进制表示形式。这样,可以更轻松地了解实际位值是什么,因此更有用。但仍然相当简洁(事实上,比十进制表示的字符数要少)。

I'd consider having __str__ return a hexadecimal representation instead. 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).

殊姿 2024-09-07 23:36:19

__repr__ 需要返回一些东西,如果传递给构造函数,将创建一个与原始对象完全相同的新对象。

您的返回“11”,但如果您将“11”传递给构造函数,您将不会得到相同的位字段作为结果。所以这个__repr__是不行的。

__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.

Yours returns '11' but if you passed '11' to your constructor you would not get the same bitfield as a result. So this __repr__ is not ok.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文