PEP? context overloading: if wants(type): ...

Terry Reedy tjreedy at home.com
Mon Aug 6 15:27:24 CEST 2001


"Jim Dennis" <jimd at vega.starshine.org> wrote in message
news:9klgl0$1frt$1 at news.idiom.com...

>  I'd propose, if I felt qualified to do so, to offer a "wants()"
>  or builtin that would allow a method to determine the desired
>  return type or "context" of an invocation.
>
>  So the use could look like:
>
> import types
>   if wants(IntType):
> return n
> elif wants(TupleType):
> return (n1,n2)
> elif wants(StringType):
> return `n`
> elif wants(VoidType): # NoneType?
> return
>
>  What am I missing about this?  What would it hurt?

a = fun(xxx) # what does a 'want'?

Without declaring types for *names* (as opposed to objects) question
is meaningless.
On the other hand, Python type objects can be passed as function
arguments to specify a return type, so no special mechanism is needed.

def f(x, typ):
  if     typ == LongType: return glong(x)
  elif  typ == FloatType: return gfloat(x)
  else return None

Terry J. Reedy






More information about the Python-list mailing list