简体   繁体   English

判别蟒对象是否是当前的__init__方法的子类

[英]discriminating whether python object is subclass of current __init__ method

This is an odd one. 这是一个奇怪的。 I have run into a situation a few times where I would like to do some action at the end of __init__ of a python object. 我遇到了几次,我想在年底做一些动作的情况__init__一个python的对象。 I would like to do the action as the last thing in the __init__ of the true class (leaf class) only. 我想这样做的动作是在最后一件事__init__只有真正的类(叶类)的。 I absolutely do not want to do it if in a superclass of self __init__ method. 我绝对不希望,如果做自我的超类__init__方法。 Is there a good way to do this? 有什么好方法吗? I grant it may be a silly thing to do. 我承认它可能做的傻事。 But it has me curious whether and how it could be done. 但它也有我好奇它是否以及如何做。

If you want some code to run in the leaf class, at the end of __init__ , without it running in the base class… just override __init__ in the leaf class. 如果你想一些代码,在叶类中运行,在结束__init__ ,没有它在基类中运行...只是重写__init__在叶类。 For example: 例如:

class Base(object):
    def __init__(self):
        print('Base initializing')
class Intermediate(Base):
    pass
class Leaf(Intermediate):
    def __init__(self):
        super(Leaf, self).__init__()
        print('Leaf initializing')

>>> o = Base()
Base initializing
>>> o = Intermediate()
Base initializing
>>> o = Leaf()
Base initializing
Leaf initializing

If you're trying to programmatically detect, from within a method like Base.__init__ , whether self is a Base or some subclass of Base … well, usually this is a bad idea, and you actually want to use method overriding, as above. 如果你想以编程方式检测,从方法内等Base.__init__ ,无论是self是一个Base或某些子类Base ......好了,通常这是一个坏主意,你真的想使用方法重载,如上。 But it can be done easily. 但它可以很容易地完成。 For example: 例如:

class Base(object):
    def __init__(self):
        if type(self) != Base:
            print('Some subclass of Base initializing')
class Leaf(Base):
    pass

>>> obj = Leaf()
Some subclass of Base initializing

(If you're worried that someone might subvert your hierarchy such that some object that's neither a Base nor a subclass of it might end up calling Base.__init__ , and you're sure that would be an evil thing rather than a clever monkeypatching thing, you can always check issubclass .) (如果你担心有人可能会颠覆你的层次,使得一些对象,既不是Base ,也没有它的一个子类,可能最终会调用Base.__init__ ,而你肯定那将是一个邪恶的东西,而不是一个聪明的monkeypatching事,可以随时查询issubclass 。)


If you're trying to programmatically detect, within a method like Base.__init__ , whether base is a "real class"… well, you need to define what that means. 如果你想以编程方式检测,方法内等Base.__init__ ,基地是否是“真正一流” ......好吧,你需要定义什么意思。 Does it mean "Not an abstract base class in the PEP3119 sense"? 是否表示“不是PEP3119意义上的抽象基类”? "Has no subclasses defined (yet)"? “有没有子类中定义(还)”? "Has an implementation for some critical method"? “是否有一些关键方法的实现”? Whatever you want to do is probably doable, but some will be harder than others. 您想做的任何事都可能是可行的,但有些会比其他的难。 (For example, to detect whether anyone has subclassed your class, you'll likely need to build a custom metaclass.) (例如,以检测是否有人子类类,你可能需要建立一个自定义的元类。)

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

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