Adding item in front of a list

Tim Peters tim_one at email.msn.com
Fri Apr 11 01:10:17 EDT 2003


[Tim]
> Guido says he also wishes list.insert() had been defined with the
> arguments in the opposite order, so that list.insert(object) could
> have a natural default index argument of 0.  I'd like to change that
> too, but it's clearly too late for that one.

[Duncan Booth]
> I don't actually see the problem with this.

"this" doesn't make sense except as referring to Guido's desire that the
arguments had been defined in the other order.  That's what it's too late
for.

> list.insert() currently takes exactly two arguments, therefore there
> would be no backwards compatibility issues with allowing it to take
> one or two arguments where a single argument acts as though you had
> given two but the first defaulted to 0.

That's a different scheme than the one mentioned.  It violates the "natural"
in "have a natural default index argument of 0".

> Just like 'range' does in fact!

That's right, and Guido regrets range's unnatural definition.  He won't add
another like that.

    def insert(self, object, index=0):
        # insert object at self[index:index]

would have been natural.

    # Since the second argument to insert is optional, its default
    # must be specified as an object a user can't possibly pass,
    # else the "but I didn't want a second argument" case can be
    # confused with the two-argument case.
    _unique = []
    def insert(self,
               usually_index_but_object_if_2nd_arg_missing,
               object_if_specified_at_all=_unique):
        if object_if_specified_at_all is _unique:
            object = usually_index_but_object_if_2nd_arg_missing
            index = 0
        else:
            object = object_if_specified_at_all
            index = usually_index_but_object_if_2nd_arg_missing
        # insert object at self[index:index]

is a conceptual mess.

While it's a popular pastime on c.l.py <wink>, Guido rarely goes for
spelling ideas whose only virtue is that they assign a strained meaning to a
previously forbidden or undefined construct.  For that reason, only
rightmost arguments ever become defaultable, and new defaultable arguments
always come after previously accepted arguments.

living-proof-that-people-can-learn-to-type-0-when-they-want-0-ly y'rs
    - tim






More information about the Python-list mailing list