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

  • Home
  • SEARCH
  • 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 975869
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 16, 20262026-05-16T03:39:35+00:00 2026-05-16T03:39:35+00:00

Given an arbitrary python object, what’s the best way to determine whether it is

  • 0

Given an arbitrary python object, what’s the best way to determine whether it is a number? Here is is defined as acts like a number in certain circumstances.

For example, say you are writing a vector class. If given another vector, you want to find the dot product. If given a scalar, you want to scale the whole vector.

Checking if something is int, float, long, bool is annoying and doesn’t cover user-defined objects that might act like numbers. But, checking for __mul__, for example, isn’t good enough because the vector class I just described would define __mul__, but it wouldn’t be the kind of number I want.

  • 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-05-16T03:39:36+00:00Added an answer on May 16, 2026 at 3:39 am

    Use Number from the numbers module to test isinstance(n, Number) (available since 2.6).

    >>> from numbers import Number
    ... from decimal import Decimal
    ... from fractions import Fraction
    ... for n in [2, 2.0, Decimal('2.0'), complex(2, 0), Fraction(2, 1), '2']:
    ...     print(f'{n!r:>14} {isinstance(n, Number)}')
                  2 True
                2.0 True
     Decimal('2.0') True
             (2+0j) True
     Fraction(2, 1) True
                '2' False
    

    This is, of course, contrary to duck typing. If you are more concerned about how an object acts rather than what it is, perform your operations as if you have a number and use exceptions to tell you otherwise.

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

Sidebar

Related Questions

Given some arbitrary SQL I would like to get the data types of the
Given two planes positioned and rotated arbitrary to each other, I'm trying to determine
Given an arbitrary number of seconds, how can I get the number of years,
Given an arbitrary R object, how can I obtain all the methods associated with
Given an arbitrary object: class Val(object): def __init__(self): this_val = 123 I want to
I want to write a function in python that can take an arbitrary number
Given an arbitrary array of size n , I'd like to reorganize the elements
I have a list of many Python objects like this: class RangeClass(object): def __init__(self,address,size):
Given an arbitrary floating point number, say -0.13 , suppose we have an algorithm
Given an arbitrary number represented in the IEEE-754 single-precision format (commonly known as float

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.