on-internet
← Community

Class() vs self.__class__() when creating object?

Stack Overflow ·

What are the advantages/disadvantages of using Class() or self.__class__() to create a new object within a class? Is one way generally preferred over the other? Here is a contrived example of what I am talking about. class Foo(object): def __init__(self, a): self.a = a def __add__(self, other): return Foo(self.a + other.a) def __str__(self): return str(self.a) def add1(self, b): return self + Foo(b) def add2(self, b): return self + self.__class__(b)

Comments

Sign in or sign up to comment and upvote.

No comments yet.