我对python编程颇为陌生,我来自Unix/Linux管理和shell脚本背景。我试图用python编写一个程序,它接受命令行参数,并根据类型(int,str)执行某些操作。但在我的情况下,输入总是被当作字符串处理。请咨询。Python中位置参数的类型
#!/usr/bin/python
import os,sys,string
os.system('clear')
# function definition
def fun1(a):
it = type(1)
st = type('strg')
if type(a) == it:
c = a ** 3
print ("Cube of the give int value %d is %d" % (a,c))
elif type(a) == st:
b = a+'.'
c = b * 3
print ("Since given input is string %s ,the concatenated output is %s" % (a,c))
a=sys.argv[1]
fun1(a)
为什么不使用['argparse'(http://docs.python.org/2.7/library/argparse.html)? – jamylak 2013-04-30 06:59:22
你可以尝试一个isinstance(,)在if - elif - else –
techiev2
2013-04-30 06:59:51
我同意TechieV2。 isinstance()可能是最简单的方法。 – pypat 2013-04-30 07:01:01