I have a class that I want it to accept an instance of that same class as initialization; in such case, it will simply return that instance.
The reason is that I want this class to accept a myriad of initialization values and then the proceeding code can use this as an object with known properties, independent on how it was initialized.
I have thought of something like:
class c(object):
def __new__(cls, *args, **kwargs):
if isinstance(args[0], c):
return args[0]
else:
return super(c, cls).__new__(cls, *args, **kwargs)
The problem is that I don't want __init__() to be called when initialized in this manner. Is there any other way?
Thanks!
__init__()to be called? Would it be OK if__init__()were called, but didn't do anything?__init__()should be called only when args[0] is not of the classifclause checked twice in two different functions, but it could look better than factory or initializing in__new__, and simpler than metaclassing. Thanks everyone!