views:

309

answers:

5

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.

+11  A: 

This is a good example where exceptions really shine. Just do what you would do with the numeric types and catch the TypeError from everything else.

But obviously, this only checks if a operation works, not whether it makes sense! The only real solution for that is to never mix types and always know exactly what typeclass your values belong to.

THC4k
+1 for Duck Typing: it doesn't matter what type my data is, just whether or not I can do what I want with it.
systempuntoout
This was the traditional approach, but ABCs have been introduced in good part to get *away* from pure duck typing and shift some distance towards a world where `isinstance` can actually be _useful_ in many cases (=="check it makes sense" as well as formal applicability of operations). Difficult shift for long-time Python-only people, but a very important subtle trend in Python's philosophy that it would be a serious error to ignore.
Alex Martelli
@Alex: True and I love typeclasses ( mostly `collections.Sequence` and friends). But afaik, there are no such classes for numbers, vectors or any other mathematical objects.
THC4k
Nothing against duck typing. It's what i would do. But there is an abstract base class for numbers: numbers.Number.
Steven Rumbalski
+1  A: 

Probably it's better to just do it the other way around: You check if it's a vector. If it is, you do a dot product and in all other cases you attempt scalar multiplication.

Checking for the vector is easy, since it should of your vector class type (or inherited from it). You could also just try first to do a dot-product, and if that fails (= it wasn't really a vector), then fall back to scalar multiplication.

sth
I'm out of daily votes but if I remember, +1 because this was going to be my answer.
aaronasterling
A: 

For the hypothetical vector class:

Suppose v is a vector, and we are multiplying it by x. If it makes sense to multiply each component of v by x, we probably meant that, so try that first. If not, maybe we can dot? Otherwise it's a type error.

EDIT -- the below code doesn't work, because 2*[0]==[0,0] instead of raising a TypeError. I leave it because it was commented-upon.

def __mul__( self, x ):
    try:
        return [ comp * x for comp in self ]
    except TypeError:
        return [ x * y for x, y in itertools.zip_longest( self, x, fillvalue = 0 )
katrielalex
if `x` is a vector then `[comp * x for comp in self]` will yield the outer product of `x` an `v`. This is a rank 2 tensor, not a scalar.
aaronasterling
change "not a scalar" to "not a vector". at least not in the original vector space.
aaronasterling
Heh, actually we're both wrong. You're assuming that `comp*x` will scale `x` by `comp`, I was assuming that it would raise a TypeError. Unfortunately, it will actually concatenate `x` with itself `comp` times. Oops.
katrielalex
meh. if `x` is a vector then it should have a `__rmul__` method (`__rmul__ = __mul__`) so that `comp * x` should scale `x` in the same way that `x * comp` is apparently intended to.
aaronasterling
+6  A: 

You want to check if some object

acts like a number in certain circumstances

If you're using Python 2.5 or older, the only real way is to check some of those "certain circumstances" and see.

In 2.6 or better, you can use isinstance with numbers.Number -- an abstract base class (ABC) that exists exactly for this purpose (lots more ABCs exist in the collections module for various forms of collections/containers, again starting with 2.6; and, also only in those releases, you can easily add your own abstract base classes if you need to).

Bach to 2.5 and earlier, "can be added to 0 and is not iterable" could be a good definition in some cases. But, you really need to ask yourself, what it is that you're asking that what you want to consider "a number" must definitely be able to do, and what it must absolutely be unable to do -- and check.

This may also be needed in 2.6 or later, perhaps for the purpose of making your own registrations to add types you care about that haven't already be registered onto numbers.Numbers -- if you want to exclude some types that claim they're numbers but you just can't handle, that takes even more care, as ABCs have no unregister method [[for example you could make your own ABC WeirdNum and register there all such weird-for-you types, then first check for isinstance thereof to bail out before you proceed to checking for isinstance of the normal numbers.Number to continue successfully.

BTW, if and when you need to check if x can or cannot do something, you generally have to try something like:

try: 0 + x
except TypeError: canadd=False
else: canadd=True

The presence of __add__ per se tells you nothing useful, since e.g all sequences have it for the purpose of concatenation with other sequences. This check is equivalent to the definition "a number is something such that a sequence of such things is a valid single argument to the builtin function sum", for example. Totally weird types (e.g. ones that raise the "wrong" exception when summed to 0, such as, say, a ZeroDivisionError or ValueError &c) will propagate exception, but that's OK, let the user know ASAP that such crazy types are just not acceptable in good company;-); but, a "vector" that's summable to a scalar (Python's standard library doesn't have one, but of course they're popular as third party extensions) would also give the wrong result here, so (e.g.) this check should come after the "not allowed to be iterable" one (e.g., check that iter(x) raises TypeError, or for the presence of special method __iter__ -- if you're in 2.5 or earlier and thus need your own checks).

A brief glimpse at such complications may be sufficient to motivate you to rely instead on abstract base classes whenever feasible...;-).

Alex Martelli
But there is an ABC for Number in the numbers module. That's what the docs claim: "The numbers module (PEP 3141) defines a hierarchy of numeric abstract base classes which progressively define more operations."
Steven Rumbalski
+1. After the edits this is the best answer.
Steven Rumbalski
+10  A: 

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 '%15s %s' % (n.__repr__(), 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.

Steven Rumbalski
Ooh, so the Number typeclass *does* exist!
THC4k