简体   繁体   English

如何在 Python 中获得正确类型的 class

[英]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.

相关问题 你如何确保用户在创建 object class Python 时输入正确的类型(在我的例子中是列表) - How do you make sure that the user inputs the correct type (list in my case) when creating an object class Python 你如何基于Enum类型重载python类方法 - How do you overload python class method based on Enum type 如何在python类中获取参数以接收值? - How do you get arguments in python class to receive values? 你如何在 python 中创建一个类或函数,允许你创建具有特定特征的序列类型 - How do you create a class, or function in python that allows you to make a sequence type with specific characteristics Python:如何将Class function的参数类型指定为当前的ZA2F2ED4F8EBC2CBB4C21A9? - Python: How do you specify the parameter type of a Class function to the current class? 当您在 mingw64-win 中键入 python 程序时,如何运行它们? - How do you get python programs to run when you type them in mingw64-win? 你如何在Python中子类化文件类型? - How do you subclass the file type in Python? 你如何在 Python 中给一个类型起别名? - How do you alias a type in Python? Python 类与另一个类在同一个文件中定义 - 如何访问文件中稍后定义的类? - Python class defined in the same file as another class - how do you get access to the one defined later in the file? 你如何在Python中克隆一个类? - How do you clone a class in Python?
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM