【发布时间】:2019-10-11 17:36:39
【问题描述】:
我想检查所有者类类型并允许Field 类属性与Dataset 类或其任何子类一起使用。那就是我希望其他开发人员不要意外地将Field 类与不是Dataset 子类的任何其他类一起使用。
Dataset 似乎还没有完全加载。
print(f"Field: {name!r} of {owner!r} ...")
确实表明所有者类是Dataset
但是代码在下一条语句 if issubclass(owner, Dataset): 处失败,因为 python 仍然不知道类 Dataset。
class Field:
def __set_name__(self, owner, name):
print(f"Field: {name!r} of {owner!r} ...")
self.name = name
if issubclass(owner, Dataset):
raise Exception(
f"Use the class {Field!r} only with class {Dataset!r}...."
)
class Dataset:
field = Field()
预计运行不会引发任何错误。但相反,我得到了
Field: 'field' of <class '__main__.Dataset'>...
NameError: name 'Dataset' is not defined
【问题讨论】:
-
每个字符串之前的
f是什么(去掉它会消除错误)。 -
@goodvibration 这是一个名为f-strings 的新功能,从 python 3.6+ 引入。如果使用旧版本,可能会出现编译错误。但是这段代码会抛出错误,因为
Dataset尚未完全加载。 -
我不完全确定您是否想限制
Field在Dataset之外使用。 Python 是鸭子类型的,你永远不知道其他人会想出什么。
标签: python-3.x python-3.6