Optional parameter object re-used when instantiating multiple objects

Aaron Brady castironpi at gmail.com
Mon Nov 17 02:57:07 CET 2008


On Nov 16, 4:05 am, Steven D'Aprano <st... at REMOVE-THIS-
cybersource.com.au> wrote:
> On Sun, 16 Nov 2008 07:05:51 +0000, Arnaud Delobelle wrote:
> > Steven D'Aprano <st... at REMOVE-THIS-cybersource.com.au> writes:
>
> >> On Sat, 15 Nov 2008 01:40:04 -0800, Rick Giuly wrote:
>
> >>> Hello All,
>
> >>> Why is python designed so that b and c (according to code below)
> >>> actually share the same list object? It seems more natural to me that
> >>> each object would be created with a new list object in the points
> >>> variable.
>
> >> That's not natural *at all*. You're initialising the argument "points"
> >> with the same list every time. If you wanted it to have a different
> >> list each time, you should have said so. Don't blame the language for
> >> doing exactly what you told it to do.
>
> > Come on.  The fact that this questions comes up so often (twice in 24h)
> > is proof that this is a surprising behaviour.
>
> Of course it's surprising. People make an assumption based on other
> languages. But there is nothing "natural" about that assumption. It may
> be common, but that doesn't mean it's the only way to think about it.

My point is that neither one is more natural than the other.  I think
that more than one person has shirked the burden of proof in making
claims about naturality, as well as overstepped the bounds of what a
conclusion about naturality entails.  In other words, "Prove it.  So
what?"

def f( a= [] ):
  a.append( 0 )
  return a

a= f()
b= f()
c= f( [] )

a== b!= c, because '[]' is not the default value, because '[] is not
[]'.

If I run 'a= []' ten times in a loop, '[]' is executed ten times.  If
I call 'f' ten times, '[]' is only executed once.  You do have a case
that the function is defined once, but executed ten times.

> Why assume that "make it equal to []" implies a different list every
> time, rather than that it is a specific list that happens to start off as
> []? Why isn't it equally "natural" to assume that it's the same list each
> time, and it starts off as [] but need not stay that way?

In other words, what does 'natural' mean?  Either provide an analytic
definition (strict necessary and sufficient conditions), or some
paradigm cases.  'Natural' in the sense that killing is natural?  Are
there any such senses?  Is natural always best?  Is natural always
obvious?

Oddly, http://dictionary.reference.com/browse/natural has 38
definitions in the longest entry.



More information about the Python-list mailing list