Python 类型:某物的字典
Python types: dict of something
有没有办法通知 python (3.6+) 我的属性是某物的字典?
我想要什么:
def classify(self, sentenses: dict(Sentence)): # <-- dict of Sentence
for s in sentences:
a = s.attribute
为了作为字典输入,您必须从输入中导入字典。 typing 模块还提供对其他类型的支持。如:元组、列表和联合。您可以使用这些相同的类型来指示 return 值。
from typing import Dict
class Sentence:
def __init__(self):
self.some_atr = ""
self.another_atr = ""
def classify(sentences: Dict[str, Sentence]): # <-- dict of Sentence
for k, v in sentences.items():
print(v.some_atr)
s1 = Sentence()
s1.some_atr = "First"
s2 = Sentence()
s2.some_atr = "Second"
sen = {'first': s1, 'second': s2}
classify(sen)
有没有办法通知 python (3.6+) 我的属性是某物的字典?
我想要什么:
def classify(self, sentenses: dict(Sentence)): # <-- dict of Sentence
for s in sentences:
a = s.attribute
为了作为字典输入,您必须从输入中导入字典。 typing 模块还提供对其他类型的支持。如:元组、列表和联合。您可以使用这些相同的类型来指示 return 值。
from typing import Dict
class Sentence:
def __init__(self):
self.some_atr = ""
self.another_atr = ""
def classify(sentences: Dict[str, Sentence]): # <-- dict of Sentence
for k, v in sentences.items():
print(v.some_atr)
s1 = Sentence()
s1.some_atr = "First"
s2 = Sentence()
s2.some_atr = "Second"
sen = {'first': s1, 'second': s2}
classify(sen)