Python - __get__ 没有为我的 class 调用

Python - __get__ is not called for my class

这是我的代码:

class Argument :
    def __init__( self, argType, argHelp = None ) :
        self.type = argType
        self.help = argHelp
    def __get__(self, obj, type=None):
        print("__get__")
        return self.type

a  = Argument( "my_ret_value" , argHelp = "some help text "  )

我想要的是 return my_ret_value 但我得到的是 :

<__main__.Argument instance at 0x7ff608ab24d0>

我已阅读 Python Descriptors examples,如果我更改我的代码以遵循它,它会起作用 - 但为什么我不能在我的 class 上按原样执行?还有别的办法吗?

编辑:

多谢指教,之前不是很懂。 __repr__ 可以解决我的问题吗?

我想要的是将一个字符串值更改为一个对象,我想将该对象视为一个字符串,并具有一些额外的属性。

您可以按如下方式使用描述符 class:

class B:
    a = Argument("my_ret_value", argHelp="some help text")

b = B()
b.a
# __get__
# 'my_ret_value'

当在另一个 class 上设置为 class 属性并通过所述 class 的实例访问时,将调用描述符的 __get__ 方法。见 Descriptor How-To Guide.