How do you clone a class in Python?

前端 未结 4 1932
遥遥无期
遥遥无期 2021-01-03 01:03

I have a class A and i want a class B with exactly the same capabilities. I cannot or do not want to inherit from B, such as doing class B(A):pass Still i want B to be ident

4条回答
  •  执笔经年
    2021-01-03 01:35

    maybe i misunderstood you question but what about wrapping A in B?

    class A:
    
        def foo(self):
            print "A.foo"
    
    class B:
    
        def __init__(self):
            self._i = A()
    
        def __getattr__(self, n):
            return getattr(self._i, n)
    

提交回复
热议问题