【发布时间】:2021-08-19 01:38:29
【问题描述】:
我正在继承UUID,我试图弄清楚UUID.hex 是普通成员还是用@property 装饰的方法。我不得不查看源代码才能弄清楚,这让我想知道是否还有其他方法。
>>> import uuid
>>> x = uuid.UUID('0000180000001000800000805f9b34fb')
>>> print([attr for attr in dir(x) if callable(getattr(x, attr))])
['__class__', '__delattr__', '__dir__', '__eq__', '__format__', '__ge__', '__getattribute__', '__getstate__', '__gt__', '__hash__', '__init__', '__init_subclass__', '__int__', '__le__', '__lt__', '__ne__', '__new__', '__reduce__', '__reduce_ex__', '__repr__', '__setattr__', '__setstate__', '__sizeof__', '__str__', '__subclasshook__']
>>> print([attr for attr in dir(x) if not callable(getattr(x, attr))])
['__doc__', '__module__', '__slots__', '__weakref__', 'bytes', 'bytes_le', 'clock_seq', 'clock_seq_hi_variant', 'clock_seq_low', 'fields', 'hex', 'int', 'is_safe', 'node', 'time', 'time_hi_version', 'time_low', 'time_mid', 'urn', 'variant', 'version']
@property
def hex(self):
return '%032x' % self.int
这是一个用@property 装饰的方法。我有点期待hex 会显示为callable(),但事实并非如此。有什么方法可以通过检查类或对象来判断吗?
谢谢!
【问题讨论】:
-
无法调用,因为你不能写
instance.hex() -
@Alex,谢谢,是的!
标签: python