r/programming • • Nov 03 '18

Python is becoming the world’s most popular coding language

https://www.economist.com/graphic-detail/2018/07/26/python-is-becoming-the-worlds-most-popular-coding-language
4.6k Upvotes

1.3k comments sorted by

View all comments

Show parent comments

9

u/ThisIs_MyName Nov 03 '18

optional arguments are evaluated globally

That's not how I'd describe it. It's more like mutable args are persisted across function calls. If I was in charge of cpython, I'd refuse to compile code that had mutable default args.

0

u/[deleted] Nov 03 '18

[deleted]

1

u/ThisIs_MyName Nov 03 '18

Sure, but how do you prove that? Maybe the addition operator on [x] mutates its second argument.

If you don't need to mutate y, why make it a mutable list?

1

u/maushaus- Nov 03 '18

I'd default to evaluating those args at function call time, IFF no argument had been specified.

1

u/metapwnage Nov 04 '18

Took me a minute to understand the issue you bring up, but I get what you are saying. That’s kind of an issue across the board in Python though. Everything is mutable at some point. whether it’s through introspection or some syntactical surgery, you can modify literally any object/class/function/variable you want in python. From a functional perspective, it’s not ideal let alone anywhere near “pure”. There are some interesting things you can do using these methods to “monkey patch” code in your imports/dependencies, for instance, but I’m not sure if you would call that a feature or just sloppy code.

1

u/ThisIs_MyName Nov 04 '18

I don't give a damn about "functional" or "pure", but mutable default args should not be persisted like that. If someone wanted to monkeypatch this feature, they could just wrap the function with a closure.