__bases__ doesn't work! What's next?

前端 未结 2 1550
逝去的感伤
逝去的感伤 2020-12-31 14:53

The following code doesn\'t work in Python 3.x, but it used to work with old-style classes:

class Extender:
    def extension(self):
        print(\"Some wor         


        
2条回答
  •  甜味超标
    2020-12-31 15:20

    As for me it is impossible. But you can create new class dynamically:

    class Extender(object):
        def extension(self):
            print("Some work...")
    
    class Base(object):
        pass
    
    Base = type('Base', (Base, Extender, object), {})
    Base().extension()
    

提交回复
热议问题