образец: < /p>
Код: Выделить всё
from typing import Generic, TypeVar
T = TypeVar('T')
class TryToGetTypeParameter(Generic[T]):
def __init__(self):
try:
print("On instancing:")
self._show_type_parameter()
except Exception as e:
print(e)
print("\n")
def _show_type_parameter(self):
print(self.__orig_class__.__args__[0])
print("\n")
def show_type_parameter(self):
print("After instancing:")
self._show_type_parameter()
print("\n")
instanced = TryToGetTypeParameter[int]()
instanced.show_type_parameter()
< /code>
Результат: < /p>
On instancing:
'TryToGetTypeParameter' object has no attribute '__orig_class__'
After instancing:
, но в настоящее время оно установлено после того, как нормальная конструкция объекта уже завершена
. Вы не сможете проверить our_class во время
init или new .
Подробнее здесь: https://stackoverflow.com/questions/793 ... ation-time