Python dot-equals (syntax proposal)

Patrick Maupin pmaupin at gmail.com
Sat May 1 01:08:14 EDT 2010


On Apr 30, 11:04 am, Jabapyth <jabap... at gmail.com> wrote:
> At least a few times a day I wish python had the following shortcut
> syntax:
>
> vbl.=func(args)
>
> this would be equivalent to
>
> vbl = vbl.func(args)
>
> example:
>
> foo = "Hello world"
> foo.=split(" ")
> print foo
> # ['Hello', 'world']
>
> and I guess you could generalize this to
>
> vbl.=[some text]
> #
> vbl = vbl.[some text]
>
> e.g.
>
> temp.=children[0]
> # temp = temp.children[0]
>
> thoughts?

First thought:  good luck getting something like this through.
Probably not going to happen, although I do find the idea very
intriguing.

Second thought:  I don't like the proposed syntax at all.

+=, -=, /=, *=, etc.  conceptually (and, if lhs object supports in-
place operator methods, actually) *modify* the lhs object.

Your proposed .= syntax conceptually *replaces* the lhs object
(actually, rebinds the lhs symbol to the new object).

If this were to be deemed worthy of the language, I would think a
better syntax would be something like:

  mystring = .upper()
  mystring = .replace('a', 'b')

etc.

The '=' shows clearly that mystring is being rebound to a new object.

As Steven has shown, '.' functions as an operator, so if this change
were accepted, in reality you would probably be able to write:

mystring = . upper()
mystring=.upper()

or whatever.  But the canonical form would probably be with a space
before the period but not after.

Regards,
Pat



More information about the Python-list mailing list