mutable default parameter problem [Prothon]

Dave Brueck dave at pythonapocrypha.com
Fri Jun 25 17:15:56 EDT 2004


Mark wrote:
> > Ahh...so the method name is just _spelled_ with an exclamation point?
>
> Yes, a Prothon identifier is the same as a Python identifier except that
it
> can end with an exclamation mark ( ! )or a question mark ( ? ).  These
marks
> can only appear at the end and there can only be one.  It is up to the
> programmer to make sure he/she uses them properly.
>
> Exclamation marks are to be used in identifiers if and only if it is a
> method that modifies the target in-place.
>
> Question marks are to be used on methods if and only if they return True
or
> False and nothing else.
>
> > IOW, the ! is a token you can use at the end of an identifier, but it
> > is not actually used by the language itself -
>
> No, the marks are significant to the language just as any other part of
the
> identifier is.

You cut out my example that elaborated on the questions I was asking:

1) It's just part of the name. So from the language's perspective, whether
you call it appendINPLACE or append! makes no difference?

2) (continuing on #1) that being the case, it's really just a naming
convention, correct? (because the language itself doesn't do anything with
that information - additional checking to enforce that convention, different
functionality, etc)

>From what I gather that's a "yes" to both questions.

> > Seems like a waste to reserve a
> > symbol for something so rarely needed.
>
> I disagree.  In-place modification is significant and happens often.
> Ignoring this is dangerous.

Well, now we're down to disagreeing how often it occurs. I just haven't seen
very many cases in practice where (1) I want to both do something to an
object AND have it return itself in a single step and (2) doing so can be
done in a clear way, and (3) I want to do it for a good reason rather than
just wanting to save a line of code. In the few cases I've seen so far, the
rationale has apparently been to make the code shorter, not necessarily
better.

> >> 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.
> >
> > Clearly, readability is in the eye of the beholder. :)
>
> How can you argue that the exclamation mark indicating
in-place-modification
> does not make it more readable?

(1) Because in practice I haven't seen the need for it much, so in my mind
it wouldn't be used that much (making it less familiar & therefore more work
to understand the code) and/or it encourages cramming more onto a line just
because you can.

The "print list.append!(5)" is a fine example of this IMO - you've combined
two *completely unrelated* operations for no good reason. Yes, it's just an
example, I know, but it's probably an example of how it'll be commonly
(mis)used. For every one "good" use of the ! form you'll probably have a
thousand uses where the only benefit was that it saved a line of code. <0.5
wink>

(2) It's just a naming convention, so you can't rely on it's presence or
absence as being accurate (probably not a big deal in practice, but
still...)

(3) Cases like w = x.y.z!() would confuse me, as would

ref! = obj.method!
...
x = ref!(5, 6, 7) # what the heck, x == obj?!

-Dave





More information about the Python-list mailing list