Best python way to return the initializing value of class if of that same class -


i have class want accept instance of same class initialization; in such case, return instance.

the reason want class accept myriad of initialization values , proceeding code can use object known properties, independent on how initialized.

i have thought of 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 don't want __init__() called when initialized in manner. there other way?

thanks!

you want use factory (f.e. see question details or google). or use class method want, f.e.:

class c(object):     @classmethod     def new(cls, *args, **kwargs):         if isinstance(args[0], cls):             return args[0]         else:             return cls(*args, **kwargs)  obj = c.new() obj2 = c.new(obj) 

Comments

Popular posts from this blog

jOOQ update returning clause with Oracle -

java - Warning equals/hashCode on @Data annotation lombok with inheritance -

java - BasicPathUsageException: Cannot join to attribute of basic type -