Код: Выделить всё
class A: pass
class B: pass
class Example(A, B): pass
...a lot of method
# new class B called BPlus
class BPlus: pass
# want a new class Example that base on (A, BPlus) but not copy old Example's method.
class Example(A, BPlus)
...here do not copy method
# try change __base__ but got 'readonly attribute' error
Example.__bases__ = (A, BPlus)
Зачем мне это нужно?Я использую pymysql, и мне нужно перехватывать каждую операцию, чтобы записать некоторую информацию. Я определяю базу класса на pymysql.cursors.Cursor и перезаписываю метод __getattribute__ в классе, чтобы перехватить метод выполнения.
Код: Выделить всё
from pymysql.cursors import Cursors as RawCurors
class Cursor(RawCursor):
def __init__(self, *args, **kwargs):
super().__init__(*args, **kwargs)
def __getattribute__(self, item):
"""Here I intercept `execute` method OK""
pass
Код: Выделить всё
from pymysql.cursors import DictCursorMixin
# note that Cursor is the new I defined above
class DictCursor(DictCursorMixin, Cursor):
pass
Я хочу заменить базовый класс SSCursor с RawCurors на Cursor и определить новый SSDictCursor.
Код: Выделить всё
# the old class
class SSCursor(RawCurors):
# here has many methods
pass
class SSDictCursor(DictCursorMixin, SSCursor):
pass
Подробнее здесь: https://stackoverflow.com/questions/793 ... -in-python
Мобильная версия