Python's idiom for function overloads

Philippe Fremy phil at freehackers.org
Tue Feb 1 06:10:47 EST 2005


> Frequently, in Python, code which checks for types, rather than
> checking for features, ends up being excessively restrictive and
> insufficiently general.

That's true, but checking for the exact features that are going to be 
needed by your code is usually quite complicated and painful to 
maintain. At the beginning, you need only a sequence. In the end, you 
need a mutable sequence that supports slicing, so you could go by 
requiring a list as well.

Enforcing types also brings the benefit that the program is more 
deterministic. In my experience, there is a lot more benefits to have an 
object whose type is clearly identified than to have function that 
accepts generic objects.

I would go as far as saying that variables should have immutable types. 
It is more restricting that what python provides currently, but leads to 
clearer programming practice: the intent of the developer shows up in 
the type he uses.

Of course, with languages such as OCaml, you get both of it: liberal 
typing with enforced consistency.

While Python's dynamic typing liberty is enjoyable, I think it harms 
when you start to work on big projects. It prevents you to put many 
safety assumptions which might bite you back.

	regards,

	Philippe



More information about the Python-list mailing list