I need to do one thing if args
is integer and ather thing if args
is string.
如果args是整数,那么我需要做一件事,如果args是字符串,我需要做其他事情。
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()
5 个解决方案
#1
13
First of, *args
is always a list. You want to check if its content are strings?
首先,* args始终是一个列表。您想检查其内容是否为字符串?
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.
它使用types.StringTypes,因为Python实际上有两种字符串:unicode和bytestrings - 这种方式都有效。
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)
.
在Python3中,内置类型已从类型lib中删除,并且只有一种字符串类型。这意味着类型检查看起来像isinstance(s,int)和isinstance(s,str)。
#2
1
You could also try to do it in a more Pythonic way without using type
or isinstance
(preferred because it supports inheritance):
您也可以尝试以更Pythonic的方式执行此操作而不使用type或isinstance(首选,因为它支持继承):
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.
这显然取决于do_some_other_thing()的作用。
#3
0
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.
上面我们比较变量args的类型是否与文字0是一个整数相同,如果你想知道例如类型是否为long,你要比较类型(0l)等。
#4
0
If you know that you are expecting an integer/string argument, you shouldn't swallow it into *args
. Do
如果您知道您期望一个整数/字符串参数,则不应将其吞入* args。做
def handle( self, first_arg = None, *args, **kwargs ):
if isinstance( first_arg, int ):
thing_one()
elif isinstance( first_arg, str ):
thing_two()
#5
0
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:
当然,如果整数事情正在修改列表中的值,也许您应该先检查。当然,如果你想循环遍历args并为整数和其他字符串做一些事情:
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.
当然这也假设try中没有其他操作会抛出TypeError。
#1
13
First of, *args
is always a list. You want to check if its content are strings?
首先,* args始终是一个列表。您想检查其内容是否为字符串?
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.
它使用types.StringTypes,因为Python实际上有两种字符串:unicode和bytestrings - 这种方式都有效。
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)
.
在Python3中,内置类型已从类型lib中删除,并且只有一种字符串类型。这意味着类型检查看起来像isinstance(s,int)和isinstance(s,str)。
#2
1
You could also try to do it in a more Pythonic way without using type
or isinstance
(preferred because it supports inheritance):
您也可以尝试以更Pythonic的方式执行此操作而不使用type或isinstance(首选,因为它支持继承):
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.
这显然取决于do_some_other_thing()的作用。
#3
0
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.
上面我们比较变量args的类型是否与文字0是一个整数相同,如果你想知道例如类型是否为long,你要比较类型(0l)等。
#4
0
If you know that you are expecting an integer/string argument, you shouldn't swallow it into *args
. Do
如果您知道您期望一个整数/字符串参数,则不应将其吞入* args。做
def handle( self, first_arg = None, *args, **kwargs ):
if isinstance( first_arg, int ):
thing_one()
elif isinstance( first_arg, str ):
thing_two()
#5
0
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:
当然,如果整数事情正在修改列表中的值,也许您应该先检查。当然,如果你想循环遍历args并为整数和其他字符串做一些事情:
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.
当然这也假设try中没有其他操作会抛出TypeError。