← Back to team overview

ufl team mailing list archive

Re: [Branch ~ufl-core/ufl/main] Rev 1021: Initial work on replacement for UFL set_foo functions

 

On Mon, May 02, 2011 at 02:02:02PM -0700, Johan Hake wrote:
> Run this and see whats happens:
>
> def jada(bada=[]):
>     bada.append(5)
>     return bada
>
> print jada()
> print jada()

Cool. I didn't know. Looks like a good feature that could be used to
write some obfuscated Python code. :-)

--
Anders


> Johan
>
> On Monday May 2 2011 13:55:53 Anders Logg wrote:
> > On Mon, May 02, 2011 at 01:26:58PM +0200, Martin Sandve Alnæs wrote:
> > > Declaring default arguments with mutable object like dicts such as
> > > this line is dangerous in Python.
> > > def compute_form_data(self, object_names={}, common_cell=None,
> > > element_mapping={}):
> > > If object_names or element_mapping is modified, the
> > > modifications change the default object for future calls.
> >
> > How do you mean modified? You mean the default will change if a value
> > is passed to the function with element_mapping=<something>?
> >
> > > Therefore it's a good habit to use None instead:
> > > def compute_form_data(self, object_names=None, common_cell=None,
> > >
> > > element_mapping=None):
> > >     if object_names is None: object_names = {}
> > >     if element_mapping is None: element_mapping = {}
> > >     ...
> >
> > ok.
> >



Follow ups

References