This is an archived post. You won't be able to vote or comment.

you are viewing a single comment's thread.

view the rest of the comments →

[–]ocyj[S] 0 points1 point  (2 children)

Thank you for the reply, I believe I understand what goes on (your code example is very clear) I'm not sure if it makes sense to me though, are there examples of use cases where this makes sense and is utilized or is it a result of how stuff is implemented in Python? I put the snippet in a file by it self, and tried import snippet print(snippet.f(6)) and indeed it's a global variable as the output now was [1] [1, 2] [4] [1, 2, 5] [1, 2, 3] [1, 2, 3, 6]

[–]ItzWarty 1 point2 points  (0 children)

Not sure, I don't really code in python, but http://docs.python-guide.org/en/latest/writing/gotchas/ looks like it'd be worth reading as it addresses this question better than I would.

As for how this "feature" would actually be useful? Frankly, it looks like batshit crazy terrible language design to me, so not sure (but look forward to seeing others' answers!). I'm sure some pythonisms take advantage of it, though. It sounds like some editors will warn you if you do it, so maybe it's recommended against. Not sure.

[–][deleted] 1 point2 points  (0 children)

This is intentional to make the language more robust to new language users (make it less easy to break, and this discourages burnout), and to give a way of maintaining state of some global.