[FRPythoneers] Riddle me this Batman

Mark Lutz lutz at rmi.net
Tue Nov 20 17:25:31 MST 2001


Here's one use: Because mutable default argument values 
retain state between function calls, they can be used as an
equivalent to C's "static" function variables--locals that 
don't go away on call exit.  It's a result of the default
object saved with the function in Python, but it's similar.

This may not be the greatest feature of the language, of course,
and you have to get your head around the difference between 
function def time and call time for it to make sense at all.  Classes
are usually a better way to retain state between calls in Python.  
OTOH, mutable defaults work as advertised.

--Mark Lutz  (http://www.rmi.net/~lutz)


----- Original Message ----- 
From: Eric Brunson <brunson at level3.net>
To: <frpythoneers at lists.community.tummy.com>
Cc: <kehellman at yahoo.com>
Sent: Monday, November 19, 2001 9:56 PM
Subject: Re: [FRPythoneers] Riddle me this Batman


> 
> I can't imagine why anyone would want that behavior with a function or
> a class.  What's the rationale for it?
> 
> * Jeffery D. Collins (jcollins at boulder.net) [011119 21:27]:
> > 
> > Once.  You can check it out by running a simple experiment:
> > 
> > class T:
> >       def x(self, q=[]):
> >   self.q = q
> >   self.q.append("x")
> > 
> > 
> > t = T()
> > t.x()
> > t.x()
> > print t.q
> > #['x', 'x']
> > 
> > s = T()
> > s.x()
> > s.x()
> > print t.q
> > #['x', 'x', 'x', 'x']
> > 
> 
> 
> -- 
>  Eric Brunson   brunson at level3.net   page-eric at level3.net  
> tcA thgirypoC muinelliM latigiD eht detaloiv tsuj evah uoY
> _______________________________________________
> 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




More information about the FRPythoneers mailing list