Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • SEARCH
  • Home
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 8134275
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 6, 20262026-06-06T09:57:21+00:00 2026-06-06T09:57:21+00:00

I found some old Python code that was doing something like: if type(var) is

  • 0

I found some old Python code that was doing something like:

if type(var) is type(1):
   ...

As expected, pep8 complains about this recommending usage of isinstance().

Now, the problem is that the numbers module was added in Python 2.6 and I need to write code that works with Python 2.5+

So if isinstance(var, Numbers.number) is not a solution.

Which would be the proper solution in this case?

  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-06-06T09:57:23+00:00Added an answer on June 6, 2026 at 9:57 am

    In Python 2, you can use the types module:

    >>> import types
    >>> var = 1
    >>> NumberTypes = (types.IntType, types.LongType, types.FloatType, types.ComplexType)
    >>> isinstance(var, NumberTypes)
    True
    

    Note the use of a tuple to test against multiple types.

    Under the hood, IntType is just an alias for int, etc.:

    >>> isinstance(var, (int, long, float, complex))
    True
    

    The complex type requires that your python was compiled with support for complex numbers; if you want to guard for this use a try/except block:

    >>> try:
    ...     NumberTypes = (types.IntType, types.LongType, types.FloatType, types.ComplexType)
    ... except AttributeError:
    ...     # No support for complex numbers compiled
    ...     NumberTypes = (types.IntType, types.LongType, types.FloatType)
    ...
    

    or if you just use the types directly:

    >>> try:
    ...     NumberTypes = (int, long, float, complex)
    ... except NameError:
    ...     # No support for complex numbers compiled
    ...     NumberTypes = (int, long, float)
    ...
    

    In Python 3 types no longer has any standard type aliases, complex is always enabled and there is no longer a long vs int difference, so in Python 3 always use:

    NumberTypes = (int, float, complex)
    

    Last but not least, you can use the numbers.Numbers abstract base type (new in Python 2.6) to also support custom numeric types that don’t derive directly from the above types:

    >>> import numbers
    >>> isinstance(var, numbers.Number)
    True
    

    This check also returns True for decimal.Decimal() and fractions.Fraction() objects.

    This module does make the assumption that the complex type is enabled; you’ll get an import error if it is not.

    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I found some mapkit code on the internet that looks like this: - (void)recenterMap
In some old Java code, I found a class that contains a lot of
I found some old code which I'm not sure I understand completely. The folowing
I've found myself running back through some old 3.5 framework legacy code, and found
I was looking into some old code in my product and i found following
I'm revisiting some old code, and have found it doesn't work with jQuery 1.6
I found some confusing code during code review and am a bit puzzled. Doing
Found some old code, circa VS 2003. Now I have just VS 2008 (SP1)
I found some jQuery code as a result of a Google search that allowed
I found this statement is some old code and it took me a second

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.