On 19/11/2015 01:59, Chris Angelico wrote:
On Thu, Nov 19, 2015 at 12:41 PM, BartC <b...@freeuk.com> wrote:
On 18/11/2015 23:22, Chris Angelico wrote:
On the contrary, it is certain always to be that exact object.
But, not the same value that appears as the default?
Nope. Mutable objects are never guaranteed to retain the same values.
That's kinda the point.
But, presumably you would expect:
a = []
to always assign an empty list to a? You don't expect this:
a = []
a.append(10)
b = []
that b now has the same value of a, namely [10]. Fortunately this isn't
the case.
Sorry, I didn't understand any of that.
If you want to think of function defaults as being values, then yes.
But they're not. They're objects.
That's not much help to someone who *does* want the default to supply
the same missing value that they don't want to bother remembering and/or
writing in the call.
Surely the language is trying to help people not hinder. How many times
after all is this behaviour actually wanted? It's the equivalent of:
fndefault=[]
def fn(a=fndefault):
a.append(10)
return a
where it is known that a and fndefault share the same mutable data.
(Although I bet some people are still surprised by that!)
I suspect those same people (unless they are experts in the murky corners of
the language) would expect:
....
and not be dependent on fn's entire call history.
Tell me, do you expect these to do the same thing?
x = []
fn(x)
fn(x)
fn(x)
# or
fn([])
fn([])
fn([])
No I don't. But I expect the version with the default argument to be
exactly the same as the last lot of calls, namely for:
fn()
fn()
fn()
to be equivalent to:
temp=[]
fn(temp)
temp=[]
fn(temp)
temp=[]
fn(temp)
The distinction is exactly the same. If you can understand that the
first one constructs a single object and uses it three times, then you
should be able to understand that the function default is also
constructed once and used every time.
As I said, it's bizarre. It means that for certain types, Python doesn't
have a default that works per call, but only a default that works once
per program.
--
Bartc
--
https://mail.python.org/mailman/listinfo/python-list