[New-bugs-announce] [issue13264] Monkeypatching using metaclass

Artem Tomilov report at bugs.python.org
Tue Oct 25 14:45:05 CEST 2011

New submission from Artem Tomilov <scraplesh at gmail.com>:

from abc import ABCMeta

class Meta(ABCMeta):
    def __instancecheck__(cls, instance):
        # monkeypatching class method
        cls.__subclasscheck__ = super(Meta, cls).__subclasscheck__
        return super(Meta, cls).__instancecheck__(instance)

    def __subclasscheck__(cls, sub):
        return cls in sub.mro()

class A(object):
    __metaclass__ = Meta

class B(object): pass

# registering class 'B' as a virtual subclass of 'A'

>>> issubclass(B, A)
>>> isinstance(B(), A) # => method __subclasscheck__ is now monkeypatched
>>> issubclass(B, A) # => desire to get 'True' because 'B' is a virtual subclass

components: None
messages: 146366
nosy: Artem.Tomilov
priority: normal
severity: normal
status: open
title: Monkeypatching using metaclass
type: behavior
versions: Python 2.7

Python tracker <report at bugs.python.org>

More information about the New-bugs-announce mailing list