def setobjattr ( obj , key , value ) : try : setattr ( obj , key , int ( value ) ) except ValueError : try : setattr ( obj , key , float ( value ) ) except ValueError : # string if not number try : setattr ( obj , key , str ( value ) ) except UnicodeEncodeError : setattr ( obj , key , value )
set attribute on an object python
Sets an object attribute with the correct data type .
def setobjattr(obj, key, value):
    """Sets an object attribute with the correct data type."""
    try:
        setattr(obj, key, int(value))
    except ValueError:
        try:
            setattr(obj, key, float(value))
        except ValueError:
            # string if not number
            try:
                setattr(obj, key, str(value))
            except UnicodeEncodeError:
                setattr(obj, key, value)