开发者

How do I handle recursive repr()'s in Python?

I've written a container type in Python and I'm trying to write a robust __repr__ method that correctly handles the case where the container contains itself.

For example, here's what the built-in list does:

>>> x = []
>>> x.append(x)
>>> repr(x)
'[[...]]'

Container types written in C for CPython can achieve this functionality by using Py_ReprEnter and P开发者_开发百科y_ReprLeave. Is there equivalent functionality in pure-Python, or do I need to create my own?


If you're using Python 3 you can make use of the reprlib.recursive_repr decorator.


You can create your own, but it's a bit of a pain if you want to do it properly: you shouldn't store a ‘being repr'd’ marker flag on the object itself because that's not thread-safe. Instead you can store a thread-local set of your instances that are being repr'd.

A much cheaper solution is to depend on a built-in repr that takes care of recursion, eg.:

def __init__(self, *list):
    self._list= list
def __repr__(self):
    return 'mything('+repr(self._list)[1:-1]+')')

As long as one object in a recursion loop causes Py_ReprEnter to happen, repr can't form a complete loop.

How do I create a thread-local set of instances?

With the threading module:

class MyThing(object):
    _local= threading.local()
    _local.reprs= set()

    def __repr__(self):
        reprs= MyThing._local.reprs
        sid= id(self)
        if sid in reprs:
            return 'MyThing(...)'
        try:
            reprs.add(sid)
            return 'MyThing(%r)' % self.something
        finally:
            reprs.remove(sid)
0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜