我有一个通往通用系统路径快捷方式的枚举类:
_HOME = str(Path().home())
class Shortcuts(Enum):
RECENTS = _HOME + '/Recents'
DESKTOP = _HOME + '/Desktop'
DOCUMENTS = _HOME + '/Documents'
DOWNLOADS = _HOME + '/Downloads'
APPLICATIONS = '/Applications'
LIBRARY = '/Library'
SYSTEM = '/System'
USERS = '/Users'
TRASH = _HOME + '/.Trash'
我希望能够访问会员返回其价值而不是成员对象的位置。
print(Shortcuts.RECENTS)
> '/Users/username/Recents'
我已经尝试过覆盖 __ getItem __ , __ getAttr _________ and __ getAttribute ___________________em> 超类方法,但是在实施无需修改时就会遇到错误。
def __getattribute__(self, item): #type error: str obj not callable
return item
def __getattribute__(self, item): #type error: str obj not callable
return item.value
def __getitem__(self, item): #returns same object if item.value, item.name, ..etc
return item
我缺少什么?
您可以通过超载__repr__
方法这样来实现这一目标:
def __repr__(self):
return self._value_