I want determine the type of an attribute in a class. I am using setattr
to set the value, and I would like to check the type that is expected, so that I can properly convert a string value before calling setattr
.
How do you do this in python?
EDIT 1- Some additional information based on the answers so far:
I only know the name of the property that I want the type for, here is some code:
def populate_object_properties(values_as_strings,
object_to_populate,
properties_to_populate):
for k in properties_to_populate:
value = values_as_strings.get(k)
if value:
setattr(object_to_populate, k, value)
else:
setattr(object_to_populate, k, None)
I want to be able test that value
is the right type before I call setattr
.
Edit 2- The reason why I need to validate the type, is that I'm using Google AppEngine's db.Model as the base type for the object_to_populate
, and it doesn't like when a put a string into an int
type. I was trying to keep the question as simple as possible, but maybe that piece of information makes a difference in how someone would answer.(?)