e-mail address change (was Re: [Tutor] python's default argument
value handling in functions - weird syntax? problem grappling
with the concept)
Jeffrey Lim
jfs.world at gmail.com
Wed Feb 9 22:19:11 CET 2005
On Wed, 09 Feb 2005 14:12:30 -0700, Bob Gailer <bgailer at alum.rpi.edu> wrote:
> At 01:14 PM 2/9/2005, Jeffrey Lim wrote:
> >
> >Example 1
> > >>> def f(a,L=[]):
> >... if L==[5]:
> >... print 'L==[5] caught'
> >... print L
> >... print 'resetting L...'
> >... L=[]
> >... L.append(a)
> >... return L
> >...
> > >>> f(5)
> >[5]
> > >>> f(5)
> >L==[5] caught
> >[5]
> >resetting L...
> >[5]
> > >>> f(2)
> >L==[5] caught
> >[5]
> >resetting L...
> >[2]
>
> That works as expected. I assume you are happy with the results.
did you even read what i gave as an example properly? So tell me then
- why are you happy with the results when 'f(2)' is called? I am not
happy with the results. Calling function 'f' with argument 2 should
not (unless u are satisfied with such a result) get caught in the 'if
L==[5]'
> >So when the default value for 'L' is an empty list, the test
> >condition, _once triggered_, *always* catches?
>
> No. What leads you to think that is happening? This code empties the list
> each time you invoke it.
>
pls read http://www.python.org/doc/2.4/tut/node6.html#SECTION006710000000000000000
for the whole context to my questions.
Read also my quotes from the doc in my original post. I assume that
you can cope with the level of the presentation of my questions.
> >
> >How is this possible?
> >
> >Or even consider the example given -
> >def f(a, L=None):
> > if L is None:
> > L = []
> > L.append(a)
> > return L
> >
> >How is 'L == None' even possible all the time, given that for
> >def f(a, L=[]):
> > L.append(a)
> > return L
> >, L isn't even [] except for the first call to 'f'?
>
> From the 1st reference you cited above: 'The default values are evaluated
> at the point of function definition"
>
perhaps you might like to explain that further, seeing as how you seem
to be adopting a "I am smarter than you" attitude.
-jf
More information about the Tutor
mailing list