[FRPythoneers] Riddle me this Batman

Keith Hellman kehellman at yahoo.com
Mon Nov 19 21:17:11 MST 2001


Never mind - I stopped being lazy and fired up the interpretter; I see it is the same.
Thanks for setting me straight.

--- Keith Hellman <kehellman at yahoo.com> wrote:
> The same for class methods?  Or are the defaults set on actual instantiation?  Or do you get
> fresh
> evaluation for each call?
> 
> --- "Jeffery D. Collins" <jcollins at boulder.net> wrote:
> > 
> > The default keyword arguments in a function are evaluated only once
> > when the function is defined in the interactive interpreter or during
> > the import of a module.  This means that mutable arguments can have
> > the side effect you experienced below.  To avoid this, I usually
> > define the keyword default to a non-mutable (such as None), then check
> > for the condition in the function:
> > 
> > def func(l=None, d=None):
> >     if l is None: l = []
> >     if d is None: d = {}
> >     ...
> > 
> > 
> > On Mon, Nov 19, 2001 at 07:41:53PM -0800, Keith Hellman wrote:
> > > >>> def func( l=[], d={} ) :
> > > 	print d
> > > 	for i in l : d[i] = i
> > > 	return d
> > > 
> > > >>> func( [1,2,3,4] )
> > > {}
> > > {4: 4, 3: 3, 2: 2, 1: 1}
> > > >>> func( [5,6,7,8,9] )
> > > {4: 4, 3: 3, 2: 2, 1: 1}
> > > {9: 9, 8: 8, 7: 7, 6: 6, 5: 5, 4: 4, 3: 3, 2: 2, 1: 1}
> > > >>> 
> > > 
> > > I realize I'm still an essential newbie, but this really throws a curve ball at my whole
> > > impression of Python.  Why, on the second invokation of func() is d already valued from the
> > > previous result?  I would understand if I had passed func( [1,2,3,4], my_dict ), then func(
> > > [5,..], mydict ) - in fact this is what I would have expected.  But d should be set to {} on
> > each
> > > call, shouldn't it?
> > > 
> > > Occasionally I run across some nuance of Python that takes me a couple hours to 'reconcile'
> in
> > my
> > > head - but eventually I'm able to do so - but I just do it for this.
> > > 
> > > * Why isn't d equated to {} on each call invokation?
> > > * Since d is (I would assume) held in the local dictionary for the particular invokation
> call
> > > frame, why is its post-func value retained?   I would have thought it would be tagged for
> GC,
> > and
> > > therefore not reused.
> > > 
> > > I would appreciate any and all explanations - thanks in advance.
> > > 
> > > 
> > > =====
> > > Keith E. Hellman
> > > kehellman at yahoo.com
> > > 
> > > __________________________________________________
> > > Do You Yahoo!?
> > > Yahoo! GeoCities - quick and easy web site hosting, just $8.95/month.
> > > http://geocities.yahoo.com/ps/info1
> > > _______________________________________________
> > > This message sent by the FRPythoneers mailing list.
> > > Unsubscribe: echo unsubscribe | FRPythoneers-request at lists.community.tummy.com
> > > URL: http://lists.community.tummy.com/mailman/listinfo/frpythoneers
> > 
> > -- 
> > Jeffery Collins (http://www.boulder.net/~jcollins)
> > _______________________________________________
> > This message sent by the FRPythoneers mailing list.
> > Unsubscribe: echo unsubscribe | FRPythoneers-request at lists.community.tummy.com
> > URL: http://lists.community.tummy.com/mailman/listinfo/frpythoneers
> 
> 
> =====
> Keith E. Hellman
> kehellman at yahoo.com
> 
> __________________________________________________
> Do You Yahoo!?
> Yahoo! GeoCities - quick and easy web site hosting, just $8.95/month.
> http://geocities.yahoo.com/ps/info1
> _______________________________________________
> This message sent by the FRPythoneers mailing list.
> Unsubscribe: echo unsubscribe | FRPythoneers-request at lists.community.tummy.com
> URL: http://lists.community.tummy.com/mailman/listinfo/frpythoneers


=====
Keith E. Hellman
kehellman at yahoo.com

__________________________________________________
Do You Yahoo!?
Yahoo! GeoCities - quick and easy web site hosting, just $8.95/month.
http://geocities.yahoo.com/ps/info1



More information about the FRPythoneers mailing list