Python类型提示类作为对象或列表
我希望有一个对象的Python类型提示类作为对象或列表,python,type-hinting,Python,Type Hinting,我希望有一个对象的列表,其功能是我的列表。{attribute name}应该返回任何属性名称的值。例如m_list.weight->[23,45,78] 一种方法是创建list的DB子类。我的问题是类型暗示。例如: T = TypeVar('T') class DB(list[T], T): # The extra T is to autocomplete T's attributes. """A database."""
列表
,其功能是我的列表。
{attribute name}应该返回任何属性名称的值。例如m_list.weight
->[23,45,78]
一种方法是创建list的DB子类。我的问题是类型暗示。例如:
T = TypeVar('T')
class DB(list[T], T): # The extra T is to autocomplete T's attributes.
"""A database."""
def __getattr__(self, attr_name) -> 'DB':
"""Return a list of attribute values.
Usage:
db = DB(item1, item2, ...)
print(db.weights) # A list of the items' weights.
"""
result = DB(getattr(x, attr_name) for x in self)
return result
@dataclass
class Item:
name: str
weight: float
inventory : DB[Item] = DB([Item('Steak', 3), Item('Sauce', 0.2)])
print(inventory[0].weight) # <---- This works to autocomplete name/weight of a single item.
print(inventory. # <---- How to autocomplete name/weight here?
解决此问题的一种方法是删除DB中对T的引用,并创建DB的子类,如下所示:
class DB(list[T]): # Removed the second T.
...
class Inventory(DB[Item]):
name: DB[str]
weight: DB[float]
第一种方法更简单——如果可行的话。有解决这个问题的方法吗?显式地键入提示
库存
起作用。它会自动完成,不会抛出任何运行时错误
T = TypeVar('T')
class DB(list[T]): # The extra T is to autocomplete T's attributes.
"""A database."""
def __getattr__(self, attr_name) -> 'DB':
"""Return a list of attribute values.
Usage:
db = DB(item1, item2, ...)
print(db.weights) # A list of the items' weights.
"""
result = DB(getattr(x, attr_name) for x in self)
return result
@dataclass
class Item:
name: str
weight: float
inventory : Union[DB[Item], Item] = DB([Item('Steak', 3), Item('Sauce', 0.2)])
print(inventory[0].weight) # <---- This works to autocomplete name/weight of a single item.
print(inventory.weight # <---- This now works too!
T=TypeVar('T')
类DB(list[T]):#额外的T用于自动完成T的属性。
“”“数据库。”“”
def\uu getattr\uu(self,attr\u name)->“DB”:
“”“返回属性值的列表。
用法:
db=db(第1项、第2项……)
打印(db.weights)#项目权重列表。
"""
结果=DB(在self中,x的getattr(x,attr_名称)
返回结果
@数据类
类别项目:
姓名:str
重量:浮子
库存:联合库存[DB[项目],项目]=DB([项目('牛排',3),项目('酱',0.2)])
打印(库存[0].weight)#显式键入暗示inventory
起作用。它会自动完成,不会抛出任何运行时错误
T = TypeVar('T')
class DB(list[T]): # The extra T is to autocomplete T's attributes.
"""A database."""
def __getattr__(self, attr_name) -> 'DB':
"""Return a list of attribute values.
Usage:
db = DB(item1, item2, ...)
print(db.weights) # A list of the items' weights.
"""
result = DB(getattr(x, attr_name) for x in self)
return result
@dataclass
class Item:
name: str
weight: float
inventory : Union[DB[Item], Item] = DB([Item('Steak', 3), Item('Sauce', 0.2)])
print(inventory[0].weight) # <---- This works to autocomplete name/weight of a single item.
print(inventory.weight # <---- This now works too!
T=TypeVar('T')
类DB(list[T]):#额外的T用于自动完成T的属性。
“”“数据库。”“”
def\uu getattr\uu(self,attr\u name)->“DB”:
“”“返回属性值的列表。
用法:
db=db(第1项、第2项……)
打印(db.weights)#项目权重列表。
"""
结果=DB(在self中,x的getattr(x,attr_名称)
返回结果
@数据类
类别项目:
姓名:str
重量:浮子
库存:联合库存[DB[项目],项目]=DB([项目('牛排',3),项目('酱',0.2)])
print(inventory[0].weight)#从类型变量继承没有意义,您想使用Generic[t]
@juanpa.arrivillaga:您的意思是classDB(list[t],Generic[t]):
?这不会自动完成。从类型变量继承没有意义,您希望使用Generic[t]
@juanpa.arrivillaga:您的意思是类DB(list[t],Generic[t]):
?这不会自动完成。