我们有一些异步属性(使用@property)和一些常用的。我们必须使用 hy 来检查一个是否是异步的,或者不使用 asyncio.iscoroutine 函数。问题是我们必须使用 getattr 而不是 。运算符,因为 dot 将调用属性内部函数。我们希望在不破坏其他项目中使用的 DSL 的情况下更改我们的 hy 代码,也就是说,我们不想对属性名称使用字符串表示法(双引号),因此我们需要编写一些像 . 但在内部调用 getattr。
=> (defmacro attr [obj attr] `(getattr ~obj '~attr))
=> (attr 5 real)
Traceback (most recent call last):
File "stdin-5bbedde10a6366314d9be4bd343639582b4d0748", line 1, in <module>
(attr 5 real)
AttributeError: 'int' object has no attribute 'real'
=> (. 5 real)
5