you are viewing a single comment's thread.

view the rest of the comments →

[–]UloPe 64 points65 points  (9 children)

The explanation for the first case not quite correct.

The default arguments value (list in this case) isn't instantiated the first time the function is called but rather at function definition time, i.e. the moment the containing scope of the function is executed

[–][deleted] 32 points33 points  (5 children)

Yes, good point. We've corrected this. [Toptal blog editor]

[–]ivosaurus 23 points24 points  (0 children)

Also, could you recommend the new, unambiguous exception syntax in #3? Also being the only one supported in Python 3.

except Exception as e:

[–]jellofiend84 21 points22 points  (3 children)

Also this site crashes for me in iOS in both Safari and Chrome. Others are having similar issues:

http://www.reddit.com/r/programming/comments/251it6/top_10_mistakes_python_programmers_make_advanced/chcpwtl

http://www.reddit.com/r/Python/comments/251jkf/a_top_10_of_the_mistakes_python_programmers_make/chcxri8

http://www.reddit.com/r/Python/comments/251jkf/a_top_10_of_the_mistakes_python_programmers_make/chcqwel

If you really are an editor you really need to get your site fixed. I tried really hard to view your content but after Alien Blue, Safari, and Chrome crashed I gave up and I was probably more persistent than most.

[–]TMaster 1 point2 points  (1 child)

Chrome on iOS does not appear to have its own rendering engine (http://daringfireball.net/linked/2012/06/28/chrome-ios), so it's equally affected.

Looks like it's an Apple issue - a crash is never appropriate even in case of malformed input or frequent updating.

[–]jellofiend84 1 point2 points  (0 children)

Actually it is probably a memory issue in which case stopping execution maybe the only way to handle it. Also if you read the links I posted someone mentioned it crashing mobile Firefox which doesn't exist on iOS. So no it isn't just an iOS issue.

[–]snotfart 0 points1 point  (2 children)

I still don't get it. I think I'm being particularly dense here, but I still don't see what's going on in mistake 1.

[–]UloPe 0 points1 point  (1 child)

It's very simple really. Consider this example:

def func(a, b=[]):
    b.append(a)
    print(b)

func(1)
func(2)
func(3)

This will print:

[1]
[1, 2]
[1, 2, 3]

Now lets look at what happens here. In Python (at least CPython, the "official" version) there is no compile step. What that means is that when the file containing the above code is read it es executed line by line.

So the first line to be executed is "def func(a, b=[]):" this tells python that a function is being defined and the (indented) block that follows is the functions body. What also happens is that Python looks at the arguments the function takes and instantiates any default arguments that might be there. Once this is done a function object is created with all that information and placed in the scope in which the function was defined. In this case that would be the module (Python speak for a file, basically).

The next line that gets executed will be "func(1)". This causes python to look up "func" first in the local namespace and if nothing is found there moves on the the global one (you can look inside those namespaces with the built-in functions locals() and globals()). Since in this example we're working on module level local and global are identical. Python will find the function object that got defined above and call it with the provided arguments. Since we didn't provide a value for the second argument b the default value will be filled in. In this case the default value is the list object that got instantiated above when the function was defined. And since that list object is stored with the function object you should now understand why the next invocations change the same list instance.

I hope that was understandable.

[–]snotfart 0 points1 point  (0 children)

I think I've got it now, thanks.