Default Value

Rotwang sg552 at hotmail.co.uk
Sat Jun 22 20:49:05 EDT 2013


On 22/06/2013 19:49, Rick Johnson wrote:
> On Saturday, June 22, 2013 12:19:31 PM UTC-5, Rotwang wrote:
>>> On 22/06/2013 02:15, Rick Johnson wrote:
>>> IS ALL THIS REGISTERING YET? DO YOU UNDERSTAND?
>>
>> No, I don't. These two special cases are not sufficient
>> for me to determine what semantics you are proposing for
>> the general case.
>
> ============================================================
>   QUESTION 1:
> ============================================================
>> For example, what happens in the second
>> example if lst is rebound?
>
> Well nothing could happen, or something significant could
> happen. How do you expect me to determine an answer for
> such a general question when i have no context, or any
> algorithms to work from.

You don't have an algorithm to work from? I assumed you did. If you 
don't have an algorithm in mind as an alternative to Python's existing 
behaviour, then what exactly is your point?

Anyway, I already proposed an algorithm that is consistent with your 
examples, in

   http://mail.python.org/pipermail/python-list/2013-June/650447.html

. Is that what you have in mind?


> Maybe you are trying to set a situation that results in
> chaos, okay, then chaos will happen.

No, I'm not. I'm simply trying to find out what alternative behaviour 
you're proposing for Python, because the examples you've given so far 
are not sufficient to explain what happens in the general case.


> But who's fault is the
> chaos when a programmer is rebinding names? The programmer
> is ultimately responsible for his action.
>
>    Remember my "Apathetic Approach"?
>
> ============================================================
>   QUESTION 2:
> ============================================================
>> Does the default stay the same or does it change to the
>> new value of lst?
>
> Here you go again, you cannot seem to comprehend very simple
> concepts. My second example clearly explains what will happen
>
>>>       py> lst = range(5)
>>>       py> lst
>>>       [0, 1, 2, 3, 4]
>>>       py> def foo(arg=lst):
>>>       ...     arg.append(1)
>>>       ...     print(arg)
>>>       ...
>>>       py> foo()
>>>       [0, 1, 2, 3, 4, 1]
>>>       py> foo()
>>>       [0, 1, 2, 3, 4, 1, 1]

No, it doesn't, because lst is not rebound in your second example. As 
such, entering >>> lst = []; foo() could return [1], or it could return 
[0, 1, 2, 3, 4, 1, 1, 1]. Both of those could result from behaviours 
that gave the exact same output as the two examples you've given. It is 
literally impossible for me to know what you would like to see happen in 
this case from the limited information you've given me.

On the other hand, from the specification I gave earlier it's easy to 
see that >>> lst = []; foo() would return [1]. Is this what you want?


> Do you see how the name 'lst' is bound to a list object
> living in global module scope? Do you see how the default
> argument (named `arg`) is a reference to a global list named
> `lst`?
>
>   ARE YOU FOLLOWING ALONG SO FAR?
>
> Since the mutable arg exists OUTSIDE the subroutine, the
> subroutine merely need to provide access to the global
> *name* `lst` from within the body of the subroutine, and the
> subroutine merely do this ONCE at compile time!
>
>   ARE YOU FAMILIAR WITH THE CONCEPT OF POINTERS?

Yes, but Python doesn't have them. Are you proposing that it should have 
them, and that they should play some role in Python default binding 
behaviour? If so then a better way of making people aware of this would 
be to tell them, rather than expecting them to guess and then pretending 
that their failure to do so represents some failing on their part.


> ============================================================
>   QUESTION 3-A:
> ============================================================
>> What about if you pass a call as a default argument, and
>> then subsequently change the behaviour of the callable?
>
> Hmm, let's have a thought experiment. What if i give you a
> digital birthday card. And when you open the card you see a
> friendly note from me:
>
>   "Rotwang is my best friend in the whole wide world".
>
> But unbeknownst to you, i have actually hacked the card and
> installed a secret wifi device. I've made the message
> mutable -- hmm, surely nothing "bad" could arise from
> mutability correct?
>
>   *evil-grin*
>
> A few hours later when you open the card to show to some
> friends, you are alarmed to see a new message:
>
>   "Rotwang is a degenerative penis condition"
>
> Now, besides being embarrassed, and probably angry as hell,
> do you understand the dangers of mutable objects?

Yes. As I expect you already know, I'm well aware of the dangers of 
mutable objects. But that isn't what I'm asking you about. I'm asking 
you what method you propose to avoid one of those dangers. Why don't you 
just answer the question, instead of answering a different question I 
didn't ask?


> I'm not suggesting you don't ever want to use the power of
> mutability, no, but you do need to have a healthy respect for
> mutability -- because it can really ruin your day!
>
> ============================================================
>   QUESTION 3-B:
> ============================================================
>> Does the argument get re-evaluated every time foo() is
>> called, or is the argument guaranteed to be the same every
>> time?
>
> If the argument is an expression, like a literal, it will be
> reset to the literal each time the subroutine is called. If
> the argument is a non-local object, the argument will NOT be
> reset each time.

Notice how neither of those answers the question I asked? I asked what 
happens when the argument is a call.


> ============================================================
>   QUESTION 3-C:
> ============================================================
>> If the latter, what happens if the arguments type is
>> modified (e.g. by changing one of its class attributes)?
>> What about defining functions dynamically, with default
>> arguments that are only known at runtime? Is there any way
>> to avoid the second type of behaviour in this case? If so,
>> how? If not, isn't that likely to prove at least as big a
>> gotcha to people who don't know the rules of RickPy as the
>> problem you're trying to solve?
>
> What if X?
> What if Y?
> What i pass in a pony but i sell it to a glue factory before
> the subroutine gets called???
>
> I'm sorry but have no more patience for these ridiculous
> questions. If you want me to answer *specific* questions, then
> be sure to ask them one at a time and provide relevant code
> examples.

Please answer the question I asked in

   http://mail.python.org/pipermail/python-list/2013-June/650447.html

.



More information about the Python-list mailing list