views:

140

answers:

5

I need to do one thing if args is integer and ather thing if args is string.

How can i chack type? Example:

def handle(self, *args, **options):

        if not args:
           do_something()
        elif args is integer:
           do_some_ather_thing:
        elif args is string: 
           do_totally_different_thing()
A: 
type(variable_name)

Then you need to use:

if type(args) is type(0):
   blabla

Above we are comparing if the type of the variable args is the same as the literal 0 which is an integer, if you wish to know if for instance the type is long, you compare with type(0l), etc.

banx
I dont understand. How to use it?
Pol
Ugh. `type(2)` is `int`, but anyway, `type` is Not Good Python
katrielalex
+10  A: 

First of, *args is always a list. You want to check if its content are strings?

import types
def handle(self, *args, **options):
    if not args:
       do_something()
    # check if everything in args is a Int
    elif all( isinstance(s, types.IntType) for s in args):
       do_some_ather_thing()
    # as before with strings
    elif all( isinstance(s, types.StringTypes) for s in args):
       do_totally_different_thing()

It uses types.StringTypes because Python actually has two kinds of strings: unicode and bytestrings - this way both work.

In Python3 the builtin types have been removed from the types lib and there is only one string type. This means that the type checks look like isinstance(s, int) and isinstance(s, str).

THC4k
your right. It is list.
Pol
Is there any preference for using `isinstance(s,types.IntType)` over just `isinstance(s,int)` or is it just to be consistent with what you mentioned for the two types of strings? Just curious.
Brent Nash
Your solution is not compatible with python 3.1!
banx
@Brent Nash yeah just consistency. Run `int is types.IntType` in the interpreter.
THC4k
Actually testing for `basestring` (the superclass of `str` and `unicode`) instead of `types.StringTypes` should work as well.
Philipp
@Philipp: Using the ABC instead of Inheritance is actually more general: Anything that is registered with the `StringTypes` ABC would work in the test. It's not really a significant difference (who registers with ABCs anyways?), but it's a step in the direction of languages that do very cool things with types (see typeclasses in Haskell, which are very similar to ABCs).
THC4k
`StringTypes` is a tuple, not an ABC. (But of course you can say `types.StringTypes += int,`.)
Philipp
A: 

If you know that you are expecting an integer/string argument, you shouldn't swallow it into *args. Do

def handle( self, first_arg = None, *args, **kwargs ):
    if isinstance( first_arg, int ):
        thing_one()
    elif isinstance( first_arg, str ):
        thing_two()
katrielalex
A: 

You could also try to do it in a more Pythonic way without using type or isinstance(preferred because it supports inheritance):

if not args:
     do_something()
else:
     try:
        do_some_other_thing()
     except TypeError:
        do_totally_different_thing()

It obviously depends on what do_some_other_thing() does.

systempuntoout
A: 

No one has mentioned it, but the Easier to Ask For Forgiveness principle probably applies since I presume you'll be doing something with that integer:

def handle(self, *args, **kwargs):
    try:
        #Do some integer thing
    except TypeError:
        #Do some string thing

Of course if that integer thing is modifying the values in your list, maybe you should check first. Of course if you want to loop through args and do something for integers and something else for strings:

def handle(self, *args, **kwargs):
    for arg in args:
        try:
            #Do some integer thing
        except TypeError:
            #Do some string thing

Of course this is also assuming that no other operation in the try will throw a TypeError.

Wayne Werner
@Wayne actually i've mentioned it :)
systempuntoout