mutable default parameter problem [Prothon]
mark at prothon.org
Thu Jun 24 20:21:05 CEST 2004
"Dave Brueck" <dave at pythonapocrypha.com> wrote
> > FYI: It's not that the exclamation mark causes append to return the
> > sequence. The exclamation mark is always there and the sequence is
> > returned. The exclamation mark is the universal symbol for in-place
> > modification. This is straight from Ruby and solves the problem that
> > Guido to not allow sequences to be returned. And, yes, I do think
> > worth bragging about ;-)
> Wait, so is the exclamation point required or not? IOW, say you have a
> like this:
> class List(list):
> def append(self, what):
> list.append(self, what)
> return self
> a = List()
> b = a.append(5)
> So in the call to append, is it a.append!(5) or just a.append(5) ? If it's
> the former, then does the compiler detect that it's required because the
> function returns 'self' or is the determining factor something else?
> Or, does the append method not really return anything, and the language
> takes care of substituting in the object? (in which case, does that mean
> can override the return value by adding '!' - such that z=foo.bar!()
> the return value of bar and z references foo?)
As I said above: It's not that the exclamation mark that causes append to
return the sequence. The exclamation mark is always there and the sequence
is always returned. In Prothon (and Ruby and other languages before) the
exclamation mark is just part of the method name and is there to warn you
that in-place modification is happening.
> Personally, I don't like the modify-in-place-and-return-the-object
> 'feature' - it's not needed _that_ often, but more importantly, it makes
> code harder to read (to me at least).
If you use the Prothon append!() exactly as you use the Python append() you
will get the exact same results. This is just an extra feature for those
that want it.
Guido avoided returning values from in-place modification functions because
of the confusion as to whether in-place mods were happening or not. We have
solved that confusion with the exclamation mark. Our code is very readable
because of this.
More information about the Python-list