It's only unexpected if you didn't learn when Python instantiates default variables and how it passes them. Or put another way, if you treat Python like some other language you learned, apparently not realizing it is a different language.
Now personally I hardly every use default values, but to me, when I see this -
> I see no reason why anybody would not want the default to evaluate every time
I want to create a function that tells me the temperature - but also lets me know the temperature yesterday. Clearly, the best way for this function to work is to have a default value of the current time. Python default values simply won't work here, as the time will be when I first start the program up and not when I call the function tomorrow for the current temperature.
Why would I ever want to have the value from when I first launched the program?! Your argument that "this is how python does it" is not a very good one. Why should I have put it in the method? I want a default value! The reason for having the default value on a function in the first place is to allow me to make functions work with a default value. If this doesn't work, the feature is useless. As I said in the first post, the only reason the feature is usable at all is because, luckily, people nearly always use only numbers or strings for default values.
EDIT: Plus your observation that you rarely use default values is probably for a good reason: they don't really work. Maybe if they worked, you'd use them?
> EDIT: Plus your observation that you rarely use default values is probably for a good reason: they don't really work. Maybe if they worked, you'd use them?
Honestly, no, it's probably because I don't write methods to be generic enough.
Ignoring that,
>argument that "this is how python does it" is not a very good one
Except that if you're writing Python you have to work by Pythons rules, so yes it is.
> The reason for having the default value on a function in the first place is to allow me to make functions work with a default value
Then in python, make sure you are using datatypes that are passed by value, not by reference. That is the core of the problem and it bites everyone at some point in every language because the rules of passing by reference or value are different in a great number of languages.
def foo(name=[]):
In python, is not saying if I didn't pass something make name an empty list, it says 'if name is not defined in this scope, create an empty list.' But with Pythons rules a name[] was created in that scope when the function was defined. It was the programmers mistake for assuming otherwise.
Once again, we are writing Python here, Pythons rules are the only ones that matter. If you are getting unexpected results because you're not following the rules, the issue is yours. Python is not called C#, or Java, or C or Ruby or whatever because it is not C# or Java or whatever.
time.time() is a specific case, but let's say I had very_expensive_function_to_call() as a default value. I would want that function to be called once at parse time, and the value to be kept going forward, instead of calling the function each time.
Both ways have their advantages, python (and the python implementations) just fell one way instead of another.
> Or put another way, if you treat Python like some other language you learned, apparently not realizing it is a different language.
No, it's not "some other language", it's every other language. Why? Because the behavior being described isn't really a default. It's more like a static variable.
Now personally I hardly every use default values, but to me, when I see this - > I see no reason why anybody would not want the default to evaluate every time
It means you should have put it in the method.