【发布时间】:2019-10-25 04:08:36
【问题描述】:
看起来很简单,但是如果只给定一个类型,我不知道如何获取类构造函数。 为了清楚起见,我创建了一个代码 sn-p,将问题作为函数存根演示。
Google 没有提供任何有用的信息,也许我找不到合适的关键字来避免 SEO 将我推向流行的 Python 网站介绍。
from typing import NamedTuple
class Person(NamedTuple):
name: str
age: int
def magic_thing_maker(the_type, ctor_args):
pass # I am stuck with what to do here
some_type = type(Person)
the_person = magic_thing_maker(some_type, {'name':'Saul Goodman',
'age':37})
assert (the_person == Person('Saul Goodman', 37))
请随时致电我的 unpythonic 询问此问题;我知道问这个问题表明我应该为手头的问题寻求更健康的方法。
非常感谢任何帮助。
【问题讨论】:
标签: python class reflection constructor