[Python-ideas] Shuffled

Steven D'Aprano steve at pearwood.info
Wed Sep 7 22:00:51 EDT 2016


On Wed, Sep 07, 2016 at 11:43:59PM +0200, Sven R. Kunze wrote:

> BUT experienced devs also need to recognize and respect the fact that 
> younger/unexperienced developers are just better in detecting 
> inconsistencies and bloody work-arounds.

That is not a fact. It is the opposite of a fact -- inexperienced 
developers are WORSE at spotting inconsistencies, because they don't 
recognise deep consistencies.

For example, the on-going controversy about mutable default arguments:

    def func(x=0, y=[]):
        x += 1
        y.append(1)
        print(x, y)

To the inexperienced Python developer this makes no sense. Why does 
Pythod reset the value of x by default, while y remembers the value it 
had from the previous call? That's an inconsistency.

But the more experienced dev realises that there is no inconsistency. 
Python behaves exactly the same way in both cases. Both x and y are 
treated the same: they both are treated as "Early Binding". The only 
difference is that 0 is immutable, and so x += 1 binds a new value to x, 
while [] is mutable, and y.append(1) modifies that value in place.

Try this instead:

    def func(x=0, y=[]):
        x += 1
        y = y + [1]  # don't use +=
        print(x, y)

The more experienced programmer might not *like* this behaviour, they 
may wish that Python used Late Binding instead, but it takes a very 
subtle and deep knowledge of the language to understand that this is not 
an inconsistency despite the superficial appearances.



-- 
Steve


More information about the Python-ideas mailing list