Well - there is 'hasattr()', so for user classes you can check for
hasattr( thing, '__add__' ). The builtins types have to be a special
case test. But contrary to my expectation, hasattr( 1, '__add__' )
didn't raise an exception and complain about not being a class or
instance - it just returned NO:
>>> hasattr( 1, '__add__' )
hasattr( 1, '__add__' )
0
>>>
But your syntax would HAVE to be a builtin operator to keep
allowed( x + 1 ) from evaluating "x + 1" first. I assume this
is the problem you are refering to below.
But the real problem is the one Tim brought up - if may be
"allowed" , but what does it _mean_, and how can you tell
until you try it? You want to know: (1) if "x" is either a
builtin type that supports "+" or hasattr( x, '__add__' );
(2) can type x coerce '1' into the necessary type ? ; AND
(3) does 'x + 1' REALLY MEAN what I think it does?.
I thought I was getting around that by stating that
"isnumeric(x) == True" means that the implementor of
it's class has PROMISED that "x + 1" does, in fact,
mean something reasonable. I now have some doubts that
it can do all that I had hoped. :-(
> There's a potential implementation problem with allowed(), though,
> depending on how syntax analysis/execution work. Hmpf, where is Guido
> when you need him...
According to his vacation message, I think he is somewhere in the
USA right now. Guido - If you had only told us in advance, we would
have organized a fan-club meeting!
- Steve M.