[Python-ideas] Operator for inserting an element into a list
Steven D'Aprano
steve at pearwood.info
Sun Jun 17 09:38:23 EDT 2018
On Sun, Jun 17, 2018 at 02:43:16PM +0300, Mikhail V wrote:
> On Sun, Jun 17, 2018 at 2:52 AM, Steven D'Aprano <steve at pearwood.info> wrote:
> > On Sat, Jun 16, 2018 at 08:21:42PM +0300, Mikhail V wrote:
> >
>
> >> By L[] there is some mnemonical hint because [] is used to create
> >> new empty list.
> >
> > How is that a hint? What is the connection between "append an item" and
> > "create a new empty list"?
>
> Where did I say it has _direct_ connection?
I didn't mention "direct" connection. You said there is a mnemonic from
"the empty list" to "L[] is used for insert". I don't know what that
mnemonic is, and your description below doesn't make sense to me.
Insert/append doesn't just work on empty lists, so the connection
between "empty list" and inserting/appending is pretty tenuous. Aside
from the presence of a list, I don't see any connection at all.
If we think about this example:
L = ["starting", "values", "go", "here"]
for item in extra_values:
L[] = item
do you still see a connection between a non-empty list and L[] used for
append?
> It has some associative connection - 'new item', 'special index case'.
> L = [] is new list which is supposed to be filled with something.
I don't see any connection between "new item" and "special index case"
either.
> And it has references to existing syntax, e.g. slice assignment, or
> adding dictionary item can be written as :
>
> mydict[key] = value
>
> so:
> mylist[] = item
>
> is not THAT far.
I would expect mylist[] = value to mean that the contents of mylist is
replaced with a single item.
And then I would wonder why this is so special that it needs dedicated
syntax, when we can already do it with existing syntax:
mylist[:] = [item]
The idea that assignment to mylist[] means "append to the end of the
list" would never cross my mind in a million years.
> I would even say it's very close - but I'm pretty
> sure you can find something against this as well.
>
> > Here is a radical thought... why don't we give lists a method that
> > inserts items at the end of the list? We could call it something like
> > "append", and then instead of hoping people guess what the syntax does,
> > they can just look up the name of the method?
> >
> > L.append(x)
>
> Exercising in wit?
>
> How about: let's assume most people can't understand even the simplest
> new feature.
> So instead of giving clean compact assignment syntax for frequent operation,
Compact it might be:
L[] = x # seven characters, including spaces
L.append(x) # eleven characters
but I think that calling it "clean" is inappropriate. Slice notation is
already one of the trickier things for beginners to learn, and this adds
extra complexity for not much benefit.
As for "frequent operation", there are lots of frequent operations in
Python. Does every one of them deserve special syntax to make it clean?
I just opened one of my modules at random, and I don't have a single
append in that module, but I have 14 calls to string.startswith and nine
calls to kwargs.pop.
Appending to a list might be common, but I don't see that it is either
common enough or important enough to add additional syntax.
> let's force less readable method call everywhere,
I think that describing a short, self-descriptive method call like
append as "less readable" demonstrates a deep and fundamental gulf
between the style you prefer and what the rest of us prefer.
Mikhail, sometimes I wonder if you would be happier using Perl rather
than Python. Like the Perl community, you seem to have a desire for
syntax which most of us see as terse and cryptic over self-descriptive
method names.
But even Perl doesn't (so far as I know) give us special syntax to
append to an array. As far as I know, the standard way to append to an
array @arr in Perl is:
push(@arr, item);
--
Steve
More information about the Python-ideas
mailing list