1

In my functions, I check for the types of the input so that it is valid (example - for a function that checks the primality of 'n', I don't want 'n' to be inputted as a string). The problem occurs with checking for longs and ints. In Python 3.3, they removed the long-type number, so the problem occurs with this:

def isPrime(n):
    """Checks if 'n' is prime"""
    if not isinstance(n, int): raise TypeError('n must be int')
    # rest of code

This works universally for both v2.7 and v3.3. However, if I import this function in a Python 2.7 program, and enter a long-type number for 'n', like this: isPrime(123456789000), it would obviously raise a TypeError because 'n' is of the type long, not int.

So, how can I check if it is valid input for both v2.7 and v3.3 for longs and ints?

Thanks!

4
  • Do you mean isPrime(123456789000L)? Commented Feb 11, 2013 at 18:08
  • @DavidRobinson isPrime(123456789000L) and isPrime(123456789000) are essentially the same thing: isinstance(123456789000L, int) and isinstance(123456789000, int) both return False. Commented Feb 11, 2013 at 18:09
  • On what version of Python? isinstance(123456789000, int) returns True for me on both Python 2.6 and Python 2.7. Commented Feb 11, 2013 at 18:11
  • 1
    (I'm guessing the issue is between 64-bit and 32-bit Python) Commented Feb 11, 2013 at 18:11

3 Answers 3

7

A way I can think of is:

from numbers import Integral

>>> blah = [1, 1.2, 1L]
>>> [i for i in blah if isinstance(i, Integral)]
[1, 1L]

edit (after an insightful comment from @martineau)

Python 2.7:

>>> map(type, [1, 1.2, 2**128])
[<type 'int'>, <type 'float'>, <type 'long'>]

Python 3.3:

>>> list(map(type, [1, 1.2, 2**128]))
[<class 'int'>, <class 'float'>, <class 'int'>]

The example still stands that using isinstance(n, numbers.Integral) but stands more coherent.

Sign up to request clarification or add additional context in comments.

5 Comments

This is the only way you should do this.
In Python 3 I get an invalid syntax error pointing to the L in blah = [1, 1.2, 1L].
@martineau That makes sense... as there is no long type in Py3. It was to demonstrate using Py2.7 (or actually from 2.6) that it's possible to use Integral for int and long or just int...
Right...still surprises me considering the OP wanted to "check if it is valid input for both v2.7 and v3.3", so maybe using blah = [1, 1.2, 123456789000] as an example valid in both versions would have been more coherent.
@martineau yup - made an update to the post to reflect your comment - feel free to edit if you feel you can make it clearer
0
def isPrime(n):
    """Checks if 'n' is prime"""
    try:
        n = int(n)
    except:
        raise TypeError('n must be int')
    # rest of code

1 Comment

That converts strings and similiar into numbers, rather then just doing type checking -> not what the op wanted, or a good idea.
-2

From: http://docs.python.org/3.1/whatsnew/3.0.html#integers

The sys.maxint constant was removed, since there is no longer a limit to the value of integers. However, sys.maxsize can be used as an integer larger than any practical list or string index. It conforms to the implementation’s “natural” integer size and is typically the same as sys.maxint in previous releases on the same platform (assuming the same build options).

if not isinstance(n, int) or n > sys.maxsize: raise TypeError('n must be int')

may work for differentiating int and long.

2 Comments

That's the exact opposite of what the OP is requesting.
Won't converting the input to int using int(n) work in that case? Instead of TypeError, ValueError can by returned.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.