How do you clone a class in Python?

前端 未结 4 1974
遥遥无期
遥遥无期 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:46

    I guess this is not what you wanted but its what the question seems to be asking for...

    class Foo(object):
        def bar(self):
            return "BAR!"
    
    cls = type("Bar", (object,), dict(Foo.__dict__))
    
    print cls
    
    x = cls()
    print x.bar()
    

提交回复
热议问题