简体   繁体   中英

How do I tell what type of data is inside python variable?

I have a list of variables.. inside the list are strings, numbers, and class objects. I need to perform logic based on each different type of data. I am having trouble detecting class objects and branching my logic at that point.

if(type(lists[listname][0]).__name__ == 'str'): # <--- this works for strings
elif(type(lists[listname][0]).__name__ == 'object'): <--- this does not work for classes

in the second line of code above, the name variable contains "Address" as the class name. I was hoping it would contain "class" or "object" so I could branch my program. I will have many different types of objects in the future, so it's a bit impractical to perform logic on every different class name, "Address" "Person" etc

please let me know if my question needs clarification.

thanks!!

FYI: it also makes a difference if its a new-style class or not:

# python
type(1).__name__
'int'
type('1').__name__
'str'
class foo(object):
  pass
type(foo()).__name__
'foo'
class bar:
  pass
type(bar()).__name__
'instance'

If you can make sure they're all new-style classes, your method will determine the real type. If you make them old-style, it'll show up as 'instance'. Not that I'm recommending making everything all old-style just for this.

However, you can take it one step further:

type(bar().__class__).__name__
'classobj'
type(foo().__class__).__name__
'type'

And always look for 'classobj' or 'type'. (Or the name of the metaclass, if it has one.)

I think you want the isinstance function.

if isinstance(o, ClassName):

However, you'll need to first verify that o is an object, you can use type for that.

It's common in Python to use exception handling to decide which code path to take; inspecting the exact type of an object (with isinstance()) to decide what to do with it is discouraged.

For example, say that what you want to do is, if it's a string, print it in "title case", and if it's an object, you want to call a particular method on it. So:

try:
    # is it an object with a particular method?
    lists[listname][0].particularMethod()
except AttributeError:
    # no, it doesn't have particularMethod(), 
    # so we expect it to be a string; print it in title case
    print lists[listname][0].title()

If you are only interested in handling two types specifically, you could test for them explicitly using isinstance and then handle the leftovers:

import numbers

for item in list:
    if isinstance(item, basestring): # (str, unicode)
        do_string_thing(item)
    elif isinstance(item, numbers.Real): # (int, float, long)
        do_number_thing(item)
    else:
        do_object_thing(item)

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM