Python 多重继承

 

代码示例:

### ref https://www.cnblogs.com/jiangzuofenghua/p/11413777.html
class Printable:
    def _print(self):
        print(111,self.content)

class Document: #第三方库,不允许修改
    def __init__(self,content):
        self.content = content

class Word(Document): pass  #第三方库,不允许修改


class PrintableWord(Printable,Word): pass
print(222,PrintableWord.__dict__)
print(333,PrintableWord.mro())

pw = PrintableWord('test string')
pw._print()

结果:

"D:\Program Files\python_3_6_4\python.exe" D:/untitled2/xdemo.py
222 {'__module__': '__main__', '__doc__': None}
333 [<class '__main__.PrintableWord'>, <class '__main__.Printable'>, <class '__main__.Word'>, <class '__main__.Document'>, <class 'object'>]
111 test string

 

代码示例:

#### ref https://blog.csdn.net/u013008795/article/details/90412084

def printable(cls): def _print(self): print(self.content,'装饰器') cls.print = _print return cls class Document: #第三方库,不允许修改 def __init__(self,content): self.content = content class Word(Document): pass #第三方库,不允许修改 @printable #先继承,后装饰 class PrintableWord(Word): pass print(PrintableWord.__dict__) print(PrintableWord.mro()) pw = PrintableWord('test string') pw.print()

 

上一篇:Python 使用PyMySql 库 连接MySql数据库时 查询中文遇到的乱码问题(实测可行) python 连接 MySql 中文乱码 pymysql库


下一篇:python在windows下连接mysql数据库