简体   繁体   English

获取基类的当前子类

[英]Get the current subclass of the baseclass

I am trying to create custom representation of Exception following some rules. 我正在尝试按照一些规则创建Exception的自定义表示形式。 I want to create a base class which defines rules. 我想创建一个定义规则的基类。

For example: 例如:

class MyBaseClassError(Exception):
    def __init__(self, blabla):
        super(MyBaseClassError, self).__init__(self)
        self.balbla = blabla
    def __str__(self):
        return "[{}]:{}".format(self.__subclasses()[0].__name__, self.blabla)

class ConfigFileError(MyBaseClassError):
    pass

So here it will return: 因此,它将返回:

print ConfigFileError("Try again :D")
[ConfigParser]: Try again :D

I want to display the current subclass instead of self.__subclasses()[0].__name__ which is the first subclass list. 我想显示当前子类而不是self.__subclasses()[0].__name__ ,这是第一个子类列表。

This will do it: 这样做:

def __str__(self):
        return "[{}]:{}".format(self.__class__.__name__, self.blabla)

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM