def get_new_obj(obj, klass, attributes):
 kwargs = {}
 for (key, value) in list(obj.__dict__.items()):
  if isinstance(value, dict):
   kwargs[key] = value.copy()
  elif isinstance(value, (tuple, list)):
   kwargs[key] = value[:]
  else:
   kwargs[key] = value
 for (key, value) in list(attributes.items()):
  if (value is None):
   continue
  if isinstance(value, dict):
   kwargs_value = kwargs.get(key, {})
   for (key1, value2) in list(value.items()):
    if (value2 is None):
     continue
    kwargs_value[key1] = value2
   kwargs[key] = kwargs_value
  else:
   kwargs[key] = value
 return klass(**kwargs)

def get_new_obj ( obj , klass , attributes ) : kwargs = { } for ( key , value ) in list ( obj . __dict__ . items ( ) ) : if isinstance ( value , dict ) : kwargs [ key ] = value . copy ( ) elif isinstance ( value , ( tuple , list ) ) : kwargs [ key ] = value [ : ] else : kwargs [ key ] = value for ( key , value ) in list ( attributes . items ( ) ) : if ( value is None ) : continue if isinstance ( value , dict ) : kwargs_value = kwargs . get ( key , { } ) for ( key1 , value2 ) in list ( value . items ( ) ) : if ( value2 is None ) : continue kwargs_value [ key1 ] = value2 kwargs [ key ] = kwargs_value else : kwargs [ key ] = value return klass ( ** kwargs )
pass attributes from the existing object obj and attributes dictionary to a klass constructor .