gaogaotiantian / objprint

A library that can print Python objects in human readable format
Apache License 2.0
510 stars 43 forks source link

type 类创建问题 #72

Closed wuqingjing2010 closed 2 years ago

wuqingjing2010 commented 2 years ago

通过type 构建的 class 通过 op 正确打印,这个feature 能否实现?

from objprint import op

class Action:
    pass

def Category(label):
    return type("MCategory", (Action,), {"label": label,
                                        "__type__": "category"})

class ECategory(Action):
    def __init__(self,label):
        self.label = label
        self.__type__ = 'category'

mm = Category('test')
nn = ECategory('test')
op(mm)
op(nn)

>> <class '__main__.MCategory'>
>> <UCategory 0x1f2da7b2b00
>>   .label = 'test'
>> >
gaogaotiantian commented 2 years ago

这个和type没关系。当你定义一个class的时候,它是有自己的__str__函数的。对于有默认的输出格式的object,objprint会在默认情况下使用他们自定义的输出格式。

你可以通过op(mm, honor_existing=False)来打印class变量。

wuqingjing2010 commented 2 years ago

OK 了 多谢