PyType_Type中的tp_new指向tp_new,而这个tp_new才是class对象创建的地方 typeobject.c static PyObject * type_new(PyTypeObject *metatype, PyObject *args, PyObject *kwds) { //metatype是PyType_Type(<type 'type'>),args中包含了(类名、基类列表、属性表) PyObject *name, *bases, *dict; stati...
Py_TPFLAGS_BASETYPE | Py_TPFLAGS_DICT_SUBCLASS | _Py_TPFLAGS_MATCH_SELF,/* tp_flags */// 标记dict_traverse,/* tp_traverse */// 遍历函数dict_tp_clear,/* tp_clear */// 清除函数dict_init,/* tp_init */// __init__PyType_GenericAlloc,/* tp_alloc */// 分配内存dict_new,/* ...
# Subscripting a regular Generic subclass. _check_generic(cls, params) # 重点 return _GenericAlias(cls, params) 1. 2. 3. 4. 5. 6. 7. 8. 9. 10. 11. 12. 13. 14. 15. 16. 17. 18. 19. 20. 21. 22. 23. 24. 25. 26. 27. 28. 29. 我们关注__class_getitem__的返回结果,...
f"Parameters to {cls.__name__}[...] must all be type variables") if len(set(params)) != len(params): raise TypeError( f"Parameters to {cls.__name__}[...] must all be unique") else: # Subscripting a regular Generic subclass. _check_generic(cls, params, len(cls.__parameters_...
=len(params):raiseTypeError(f"Parameters to{cls.__name__}[...] must all be unique")else:# Subscripting a regular Generic subclass.forparamincls.__parameters__: prepare =getattr(param,'__typing_prepare_subst__',None)ifprepareisnotNone:...
(hashfunc)long_hash, /* tp_hash */ 0, /* tp_call */ 0, /* tp_str */ PyObject_GenericGetAttr, /* tp_getattro */ 0, /* tp_setattro */ 0, /* tp_as_buffer */ Py_TPFLAGS_DEFAULT | Py_TPFLAGS_BASETYPE | Py_TPFLAGS_LONG_SUBCLASS | _Py_TPFLAGS_MATCH_SELF, /* tp_...
Py_TPFLAGS_BASETYPE | Py_TPFLAGS_DICT_SUBCLASS | _Py_TPFLAGS_MATCH_SELF, /* tp_flags */ // 标记 dict_traverse, /* tp_traverse */ // 遍历函数 dict_tp_clear, /* tp_clear */ // 清除函数 dict_init, /* tp_init */ // __init__ PyType_GenericAlloc, /* tp_alloc */ // ...
defon_batch_begin(self,batch,logs=None):"""A backwards compatibility alias for `on_train_batch_begin`."""@doc_controls.for_subclass_implementers @generic_utils.default defon_batch_end(self,batch,logs=None):"""A backwards compatibility alias for `on_train_batch_end`."""@doc_controls.for_...
Genericwill have: a__class_getitem__that will return instances ofGenericAliaswhich keep track of the original class and type arguments. __init_subclass__that will properly initialize the subclasses, and perform necessary bookkeeping. GenericAliaswill have: ...
Given some base class that is generic over a param-spec, I'd like to be able to define the param-spec using a subclass method implementation. Something like: T = TypeVar("T") P = ParamSpec("P") class Base(Generic[P, T]): func: Callable[P...