Warning: file_get_contents(/data/phpspider/zhask/data//catemap/1/visual-studio-2008/2.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
在Python2.7和Python3.3之间键入检入函数_Python_Function_Types_Python 3.x_Python 2.7 - Fatal编程技术网

在Python2.7和Python3.3之间键入检入函数

在Python2.7和Python3.3之间键入检入函数,python,function,types,python-3.x,python-2.7,Python,Function,Types,Python 3.x,Python 2.7,在我的函数中,我检查输入的类型以使其有效(例如,对于检查“n”的素性的函数,我不希望将“n”作为字符串输入)。 检查longs和ints时会出现问题。 在Python 3.3中,他们删除了long-类型的编号,因此出现以下问题: def isPrime(n): """Checks if 'n' is prime""" if not isinstance(n, int): raise TypeError('n must be int') # rest of code 这对

在我的函数中,我检查输入的类型以使其有效(例如,对于检查“n”的素性的函数,我不希望将“n”作为字符串输入)。 检查
long
s和
int
s时会出现问题。 在Python 3.3中,他们删除了
long
-类型的编号,因此出现以下问题:

def isPrime(n):
    """Checks if 'n' is prime"""
    if not isinstance(n, int): raise TypeError('n must be int')
    # rest of code
这对v2.7和v3.3都通用。 但是,如果我在Python 2.7程序中导入此函数,并为“n”输入一个
long
-类型编号,如:
isPrime(123456789000)
,它显然会引发
TypeError
,因为“n”的类型是
long
,而不是
int

那么,对于
long
s和
int
s,我如何检查它对于v2.7和v3.3是否有效


谢谢

我能想到的一个方法是:

from numbers import Integral

>>> blah = [1, 1.2, 1L]
>>> [i for i in blah if isinstance(i, Integral)]
[1, 1L]
编辑(在@martineau发表见解深刻的评论之后)

Python 2.7:

>>> map(type, [1, 1.2, 2**128])
[<type 'int'>, <type 'float'>, <type 'long'>]
映射(类型[1,1.2,2**128]) [, ] Python 3.3:

>>> list(map(type, [1, 1.2, 2**128]))
[<class 'int'>, <class 'float'>, <class 'int'>]
列表(映射(类型[1,1.2,2**128])) [, ]
这个例子仍然存在,使用
isinstance(n,numbers.Integral)
,但更为连贯。

我能想到的一种方法是:

from numbers import Integral

>>> blah = [1, 1.2, 1L]
>>> [i for i in blah if isinstance(i, Integral)]
[1, 1L]
编辑(在@martineau发表见解深刻的评论之后)

Python 2.7:

>>> map(type, [1, 1.2, 2**128])
[<type 'int'>, <type 'float'>, <type 'long'>]
映射(类型[1,1.2,2**128]) [, ] Python 3.3:

>>> list(map(type, [1, 1.2, 2**128]))
[<class 'int'>, <class 'float'>, <class 'int'>]
列表(映射(类型[1,1.2,2**128])) [, ] 这个例子仍然认为使用
是恒量(n,numbers.Integral)
,但是更加连贯。

来自:

sys.maxint常量已删除,因为整数的值不再有限制。但是,sys.maxsize可以用作大于任何实际列表或字符串索引的整数。它符合实现的“自然”整数大小,通常与同一平台上早期版本中的sys.maxint相同(假设构建选项相同)

可用于区分整型和长型。

与:

def isPrime(n):
    """Checks if 'n' is prime"""
    try:
        n = int(n)
    except:
        raise TypeError('n must be int')
    # rest of code
sys.maxint常量已删除,因为整数的值不再有限制。但是,sys.maxsize可以用作大于任何实际列表或字符串索引的整数。它符合实现的“自然”整数大小,通常与同一平台上早期版本中的sys.maxint相同(假设构建选项相同)



可能有助于区分int和long。

你的意思是
isPrime(12345678900l)
?@DavidRobinson
isPrime(12345678000l)
isPrime(123456789000)
本质上是一样的:
isinstance(12345678000l,int)
isinstance(123456789000,int)
两者都返回
False
。在哪个版本的Python上
isinstance(123456789000,int)
在Python 2.6和Python 2.7上为我返回
True
(我猜问题在64位和32位Python之间)你是说
iPrime(12345678900L)
?@DavidRobinson
iPrime(12345678000L)
iPrime(123456789000)
本质上是相同的:
isinstance(123456789000,int)
isinstance(123456789000,int)
都返回
False
。在哪个版本的Python上
isinstance(123456789000,int)
在Python2.6和Python2.7上为我返回
True
(我猜问题在64位和32位Python之间)这是唯一应该这样做的方法。在Python3中,我得到一个无效的语法错误,指向
blah=[1,1.2,1L]
@martineau中的
L
,这是有意义的。。。因为Py3中没有
long
类型。这是为了证明使用Py2.7(或者实际上是从2.6开始)可以对
int
long
使用
Integral
,或者只是
int
…对……考虑到OP想要“检查它是否对v2.7和v3.3都有效输入”,我仍然感到惊讶,所以可能使用
blah=[1,1.2,123456789000]
作为一个在两个版本中都有效的例子,可能会更加连贯。@martineau yup-对帖子进行了更新以反映您的评论-如果您觉得可以使其更清晰,请随意编辑。这是唯一应该这样做的方法。在Python 3中,我在
blah=[1,1.2,1L]中遇到一个指向
L
的无效语法错误
@martineau有道理。。。因为Py3中没有
long
类型。这是为了证明使用Py2.7(或者实际上是从2.6开始)可以对
int
long
使用
Integral
,或者只是
int
…对……考虑到OP想要“检查它是否对v2.7和v3.3都有效输入”,我仍然感到惊讶,所以可能使用
blah=[1,1.2,123456789000]
作为一个在两个版本中都有效的例子,可能会更加连贯。@martineau yup-对帖子进行了更新以反映您的评论-如果您觉得可以使其更清晰,请随意编辑。这与OP的要求完全相反。在这种情况下,使用int(n)将输入转换为int不起作用吗?ValueError可以被返回,而不是TypeError。这与OP请求的正好相反。在这种情况下,使用int(n)将输入转换为int不起作用吗?可以返回ValueError而不是TypeError。这将字符串和类似项转换为数字,而不仅仅是执行类型检查->不是op想要的,或者是一个好主意。这将字符串和类似项转换为数字,而不仅仅是执行类型检查->不是op想要的,或者是一个好主意。
def isPrime(n):
    """Checks if 'n' is prime"""
    try:
        n = int(n)
    except:
        raise TypeError('n must be int')
    # rest of code