Python Forum
how to determine if an object is a number
Thread Rating:
  • 1 Vote(s) - 4 Average
  • 1
  • 2
  • 3
  • 4
  • 5
how to determine if an object is a number
#4
If you know the type. You should be able to detect it.
from decimal import Decimal
from fractions import Fraction

NUMBER = (int, float, complex, Decimal, Fraction)

def is_number(n):
    if isinstance(n, NUMBER):
        print(n, 'is a number')
    else:
        print(n, 'is not a number')

is_number(1)
is_number(1.2)
is_number(Decimal(2.02))
is_number(Fraction(2,3))
is_number(1j)
is_number('1')
99 percent of computer problems exists between chair and keyboard.
Reply


Messages In This Thread
RE: how to determine if an object is a number - by Windspar - Jul-09-2018, 11:00 PM

Possibly Related Threads…
Thread Author Replies Views Last Post
  TypeError: int() argument must be a string, a bytes-like object or a number, not 'Non Anldra12 2 5,331 May-02-2021, 03:45 PM
Last Post: Anldra12
  Determine number of all the immediately adjacent points in python zackk 1 1,940 Feb-06-2021, 09:23 AM
Last Post: zackk
  How to determine pen color from an image? robie972003 2 2,500 Mar-24-2019, 10:06 PM
Last Post: robie972003
  determine if an number is in a list Dbeah 7 3,910 Nov-06-2018, 12:11 PM
Last Post: buran
  determine if an number is in a list Dbeah 1 2,301 Nov-04-2018, 04:50 PM
Last Post: stullis
  How Do I Determine indentation in AST? jimo 3 4,360 Jul-01-2018, 04:25 PM
Last Post: buran
  The number of object makes code slower? fig0 1 2,555 Jan-25-2018, 11:16 PM
Last Post: Gribouillis
  Object Detection that records the number of secs the face is not detected trabis03 1 2,581 Jul-21-2017, 04:14 PM
Last Post: nilamo
  Determine whether a method was overridden porton 6 6,274 Nov-14-2016, 09:51 PM
Last Post: Ofnuts

Forum Jump:

User Panel Messages

Announcements
Announcement #1 8/1/2020
Announcement #2 8/2/2020
Announcement #3 8/6/2020