> George Sakkis wrote: > > "Steven Bethard" <[EMAIL PROTECTED]> wrote in message > > news:[EMAIL PROTECTED] > > > >>Is there a good way to determine if an object is a numeric type? > > > > In your example, what does your application consider to be numeric? > > Well, here's the basic code: > > def f(max=None): > ... > while max is None or n <= max: > ... > # complicated incrementing of n > > So for 'max', technically all I need is <= support. However, the code > also depends on the fact that after incrementing 'n' enough, it will > eventually exceed 'max'. Currently, ints, longs, floats, and Decimals > will all meet this behavior. But I'd rather not specify only those 4 > (e.g. with a typecheck), since someone could relatively easily create > their own new numeric type with the same behavior. Do you know a better > way to test for this kind of behavior? > > Steve
The problem is more conceptional rather than technical. So what you're saying is that 3 <= "3.0" should not be allowed, but 3 <= SomeUserDefinedNumericClass(3) is ok, although your program knows nothing a priori about SomeUserDefinedNumericClass. The idea suggested before, try if "x+1" fails or not does not get you far; any class that overrides __add__ may fool this test: class Dummy: def __add__(self,other): return 0 Is Dummy 'numeric' ? Probably not. Whether a given instance is "numeric" (or "sequence", "file-like", etc. for that matter) is a semantic issue. It cannot be resolved by relying on which methods are supported or not, but what these methods *do*; this is something that no programming language can tell you now and in the (foreseeable) future. George -- http://mail.python.org/mailman/listinfo/python-list