Python Design Principles

Neal Norwitz nnorwitz at gmail.com
Fri Sep 9 23:24:05 EDT 2005


lechequier at gmail.com wrote:
>
> But I am still puzzled by the argument that has been given for why
> methods that operate on mutable types should return None, namely, that
> the designers of python didn't want the users to shoot themselves in
> the foot by thinking a method simply returned a result and left the
> data structure unchanged.

Let me try to answer your question with a question.  Given this code:

  >>> d = {}
  >>> e = d.update([(1, 2)])

If .update() returned a dictionary, does d == e?

I'm not sure what you would guess. I am pretty sure that everyone
wouldn't agree whether d should equal e or not.  If they are not equal,
that would mean a new copy would be made on each update which could be
incredibly expensive in speed and memory.  It is also different from
how Python works today, since the update() method mutates the
dictionary.

> In the context of fundamental design principles, if you asked a random
> sample of Python gurus what is more Pythonesque: preventing users from
> shooting themselves in the foot or making things easier to accomplish,
> my impression is that people would overwhelmingly choose the latter.

Probably true, but ...

> After all, the fact that Python is not strongly typed and is
> interpreted rather than compiled gives plenty of ways for people to
> shoot themselves in the foot but what is gained is the abilitity to do
> more with less code.

I think most people programming Python are pretty pragmatic.  There is
no single language that is ideal in all circumstances.  There are
necessarily some trade-offs.  Many believe that tools can help bridge
this gap.  There are at least 2 tools for finding bugs (or gotchas) of
this sort:  pychecker and pylint.

> But in this instance, by not allowing operations on mutable types to
> return the mutated objects, it seems that the other side is being
> taken, sacrificing programmer producitivity for concerns about
> producing possible side effects. It is somewhat ironic, I think, that
> Java, a language whose design principles clearly side on preventing
> users from shooting themselves in the foot, much more so thatn Python,
> generally allows you to get back the mutated object.

I think Python has attempted to create an internal consistency.  I
believe Java has tried to do the same.  However, these aren't the same
sets of consistency.  People are always going to have assumptions.
Python strives to be as intuitive as possible.  However, it can't be
right 100% of the time for all people.

HTH,
n




More information about the Python-list mailing list