Python and generic programming
Roman Suzi
rnd at onego.ru
Fri Oct 22 04:05:41 EDT 2004
On Thu, 21 Oct 2004, Josiah Carlson wrote:
>
> Roman Suzi <rnd at onego.ru> wrote:
>
> > Most Python programs are already kinda generic, yes. But generic
> > programming adds multi-sorted types control and constraints to ensure type
> > safety...
>
> "kinda generic"? That is quite the understatement.
>
> As for "type safety", Python has runtime type checking, and as a
> programmer, if you want to have varying dispatch based on type, you are
> free to do so:
>
> foo_dispatch = {(int, int):foo_int_int, #some defined function
> ...}
>
> def foo(arg1, arg2):
> foo_dispatch[(type(arg1), type(arg2))](arg1, arg2)
>
>
> With decorator syntax, the above becomes even easier.
>
>
> Your arguments claiming Python is not generic, and your suggestions that
> Python is not type safe while being generic are unfounded. One likely
> reason why you don't find many examples of what I show above, is because
> it is usually not necessary; even for production applications.
The essense of my question was not to proof or dis-proof Python's
genericity capabilities! But I'd liked to know from those who know ;-)
can Python be called as supporting GP or not, and if not, what is missing.
C++ doesn't support GP in full, but it seems, its heading to that
direction.
As byside, type-safety is a good thing sometimes, especially in moderately
large projects. So probably interfaces or concepts is a good idea for a
PEP. And I think GPs concepts are even better than just interfaces as they
add constraints.
Right now concepts in Python are vaguely expressed like
file objects must have this, this and this.
> - Josiah
Sincerely yours, Roman A.Suzi
--
- Petrozavodsk - Karelia - Russia - mailto:rnd at onego.ru -
More information about the Python-list
mailing list