python 装饰器3 robot源码@py2to3

  1. 定义
if PY2:
    # io.StringIO only accepts u'foo' with Python 2.
    from StringIO import StringIO

    def py2to3(cls):
        if hasattr(cls, '__unicode__'):
            cls.__str__ = lambda self: unicode(self).encode('UTF-8')
        return cls

else:
    from io import StringIO

    def py2to3(cls):
        if hasattr(cls, '__unicode__'):
            cls.__str__ = lambda self: self.__unicode__()
        if hasattr(cls, '__nonzero__'):
            cls.__bool__ = lambda self: self.__nonzero__()
        return cls
  1. 使用

@py2to3
class _BaseSettings(object):
    xxx
posted @ 2021-04-06 15:31  该显示昵称已被使用了  阅读(53)  评论(0编辑  收藏  举报