Default Value

Rick Johnson rantingrickjohnson at gmail.com
Sat Jun 22 14:49:16 EDT 2013


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. 

Maybe you are trying to set a situation that results in
chaos, okay, then chaos will happen. 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] 

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?

============================================================
 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? 

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. See "ANSWER 2" for more context.
 
============================================================
 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.



More information about the Python-list mailing list