定义类的repr()(不是实例) [英] defining repr() of classes (not instances)

查看:64
本文介绍了定义类的repr()(不是实例)的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有一堆类,它们用作单例/枚举/字典键,例如G。像这样:

I have a bunch of classes which I'm using as singletons / enums / dict keys, e. g. like this:

class Side(object): pass

class Left(Side): pass
class Right(Side): pass

def show_state(distribution):
    print "left is", distribution[Left]
    print "right is", distribution[Right]

distribution = { Left: 3, Right: 7 }
show_state(distribution)

这对我来说很好。但是我有时会遇到调试输出的一个小问题。通常,我只使用 print 来实现此目的,就像 show_state()中的 print distribution 一样。 code>函数。我希望输出如下:

This works fine for me. But I'm having a small issue with debug output I sometimes do. Normally I use just print for this like in print distribution in the show_state() function. I would love to have an output like:

{ Left: 3, Right: 7 }

但是当我在这些类中执行此操作时,它们的含义如下:

But when I do this with these classes they are given out as something like this:

{<class '__main__.Right'>: 7, <class '__main__.Left'>: 3}

我试图重写类的 __ repr __()方法来实现此目的,但是当我这样做只会影响我的类的 instances (我从未创建过)。我尝试使用 @classmethod @staticmethod ,但无济于事。

I tried to override the __repr__() method of my classes to achieve this, but when I do it only influences instances of my classes (which I never create). I tried to use @classmethod and @staticmethod but nothing worked.

我假设我打印的是 Left ,因此是< type'type'> ,因此很遗憾,我必须重写 type 类的 __ repr __()方法。

I assume that what I print is a Left and therefore an instance of <type 'type'>, so I would have to override the __repr__() method of the type class which is immutable, unfortunately.

我还有其他技巧可以使用吗,以便 print distribution 可以打印我想要的东西吗?

Is there any other trick I could use so that print distribution would print what I want?

顺便说一句,根据文档, __ repr __()方法应该返回一些内容,Python解析器将再次将该内容转换为相等的对象;像< class'__main __。Right'> 这样的输出绝对不是这种情况,但像 Right这样的输出肯定是这种情况

Btw, according to the documentation, the __repr__() method should return something which the Python parser would turn into an equal object again; this is definitely not the case with an output like <class '__main__.Right'> but would definitely be the case with an output like Right.

推荐答案

您是正确的,您必须覆盖 __repr __ 类的类型;您不需要编辑 type ,您只需 subclass type 创建一个新元类

You are correct that you'd have to override the __repr__ of the type of a class; you don't need to edit type, you'd subclass type to create a new metaclass:

class SimpleRepr(type):
    def __repr__(cls):
        return cls.__name__

然后将其用作您的类元类;假设您使用的是Python 3:

then use that as your class metaclass; assuming you are using Python 3:

class Side(metaclass=SimpleRepr): pass

,或者如果您仍在使用Python 2:

or if you are using Python 2 still:

class Side(object):
    __metaclass__ = SimpleRepr

<$ c $的子类c> Side 也继承了元类:

>>> class SimpleRepr(type):
...     def __repr__(cls):
...         return cls.__name__
...
>>> class Side(metaclass=SimpleRepr): pass
...
>>> class Left(Side): pass
...
>>> class Right(Side): pass
...
>>> { Left: 3, Right: 7 }
{Left: 3, Right: 7}

但是,您可能只使用了实例:

However, you could just have used instances:

class Side(object):
    __slots__ = ('name',)
    def __init__(self, name):
        self.name = name
    def __repr__(self):
        return self.name

Left = Side('Left')
Right = Side('Right')

这篇关于定义类的repr()(不是实例)的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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