Does python support multi prototype.
Chris Dutton
rubyguru at hotmail.com
Wed Aug 4 15:11:08 EDT 2004
Jorgen Grahn wrote:
> On Wed, 04 Aug 2004 02:17:11 GMT, Chris Dutton <rubyguru at hotmail.com> wrote:
>
>>angel wrote:
>>>Does python support it?
>>
>>No, but it doesn't have to. The type of a function's arguments are not
>>statically limited.
>
> ...
>
> Yes, but that's not as elegant in cases where the arguments' types has to
> trigger very different processing. I can even imagine situations where
> foo(S) and a foo(T) do completely different things, and still 'foo' is the
> best name for both of them.
So use the existing control flow structures to decide which set of code
to execute at runtime. It's certainly not going to be as efficient as a
compiled language which can make the distinction at compile time, but
it's easily as elegant a way of thinking about the problem.
def foo(bar):
if (isinstance(bar, int)):
...
elif (isinstance(bar, str)):
...
else
...
Now, if only Python had a smart "switch" style construct like Ruby or
Groovy, since "isinstance" hurts my eyes. :-)
def foo(bar)
case bar
when Integer
...
when String
...
else
...
end
end
More information about the Python-list
mailing list