[英]How do you get the correct type of class in Python
I have a list of instances from different classes.我有一个来自不同类的实例列表。 I need to sort out which instances belong to which type.我需要整理出哪些实例属于哪种类型。 However I cannot get the type
method to work the way I want但是我无法让type
方法以我想要的方式工作
type(consec_notes[0])
<class 'music21.key.Key'>
type(consec_notes[0]) == 'music21.key.key'
False # should say True
type(consec_notes[0]) == "<class 'music21.key.Key>'"
False
You should better compare a type with his class您最好将一个类型与他的 class 进行比较
type(consec_notes[0]) == music21.key.Key
get the class name by consec_notes[0].__class__.__name__
通过consec_notes[0].__class__.__name__
获取 class 名称
>> consec_notes[0].__class__.__name__ == "music21.key.Key"
# should return True
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.