对于自定义Python类,哪种更好__repr__? [英] Which is a better __repr__ for a custom Python class?

查看:112
本文介绍了对于自定义Python类,哪种更好__repr__?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

__repr__函数似乎可以通过不同的方式返回.

It seems there are different ways the __repr__ function can return.

我有一个InfoObj类,用于存储许多内容,其中有些我特别希望该类的用户自行设置.我认识到python中没有任何东西受到保护,他们可以直接进入并进行设置,但是似乎在__init__中定义它使人们更有可能看到它,并认为将其传递就可以了.

I have a class InfoObj that stores a number of things, some of which I don't particularly want users of the class to set by themselves. I recognize nothing is protected in python and they could just dive in and set it anyway, but seems defining it in __init__ makes it more likely someone might see it and assume it's fine to just pass it in.

(示例:当验证函数确定对象已完全填充时,由验证函数设置的布尔值;在存储了足够的信息后,根据其他值计算得出的值...例如A = B + C ,因此一旦设置了A和B,便会计算出C并将对象标记为Valid = True.)

(Example: Booleans that get set by a validation function when it determines that the object has been fully populated, and values that get calculated from other values when enough information is stored to do so... e.g. A = B + C, so once A and B are set then C is calculated and the object is marked Valid=True.)

那么,考虑到所有这些,这是设计__ repr__的输出的最佳方法吗?

So, given all that, which is the best way to design the output of __ repr__?

    bob = InfoObj(Name="Bob")
    # Populate bob.

    # Output type A:
    bob.__repr__()
'<InfoObj object at 0x1b91ca42>'

    # Output type B:
    bob.__repr__()
'InfoObj(Name="Bob",Pants=True,A=7,B=5,C=2,Valid=True)'

    # Output type C:
    bob.__repr__()
'InfoObj.NewInfoObj(Name="Bob",Pants=True,A=7,B=5,C=2,Valid=True)'

...类型C的意义是不要愉快地将我在C ++中设置为私有"的所有内容用作构造函数的参数,并让使用该类的队友使用接口函数对其进行设置,即使这对他们来说是更多的工作.在这种情况下,出于__repr__

... the point of type C would be to not happily take all the stuff I'd set 'private' in C++ as arguments to the constructor, and make teammates using the class set it up using the interface functions even if it's more work for them. In that case I would define a constructor that does not take certain things in, and a separate function that's slightly harder to notice, for the purposes of __repr__

如果有什么不同,我计划使用它们的__repr__输出将这些python对象存储在数据库中,并使用eval()检索它们,至少除非我想出一种更好的方法.队友手动创建一个完整的对象而不是通过适当的界面功能进行操作的结果是,直到有人弄清楚他的操作之前,一种类型的信息检索可能都是不稳定的.

If it makes any difference, I am planning to store these python objects in a database using their __repr__ output and retrieve them using eval(), at least unless I come up with a better way. The consequence of a teammate creating a full object manually instead of going through the proper interface functions is just that one type of info retrieval might be unstable until someone figures out what he did.

推荐答案

__repr__方法旨在为开发人员而不是最终用户提供最有用的输出,因此只有您才能真正回答这个问题.但是,我通常会选择选项B.选项A并不是很有用,选项C不必要地冗长-无论如何,您都不知道如何导入模块.其他人可能更喜欢选项C.

The __repr__ method is designed to produce the most useful output for the developer, not the enduser, so only you can really answer this question. However, I'd typically go with option B. Option A isn't very useful, and option C is needlessly verbose -- you don't know how your module is imported anyway. Others may prefer option C.

但是,如果要将Python对象存储为数据库,请使用pickle.

However, if you want to store Python objects is a database, use pickle.

import pickle
bob = InfoObj(Name="Bob")

> pickle.dumps(bob)
b'...some bytestring representation of Bob...'

> pickle.loads(pickle.dumps(bob))
Bob(...)

如果您使用的是较旧的Python(-3.x之前的版本),请注意,cPickle速度更快,但是pickle可扩展性更高. Pickle无需任何配置即可在您的某些类上工作,但对于更复杂的对象,您可能需要编写自定义Pickler.

If you're using older Python (pre-3.x), then note that cPickle is faster, but pickle is more extensible. Pickle will work on some of your classes without any configuration, but for more complicated objects you might want to write custom picklers.

这篇关于对于自定义Python类,哪种更好__repr__?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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