new feature in Python.

Jason C. McDonald codemouse92 at outlook.com
Wed Sep 30 12:13:24 EDT 2020


> I have a great idea, there are __iX__` methods, such as `__ior__`,
> `__iadd__`, `__iand__` etc.., which implements the |=, +=, &=
> behavior, 
> it would be nice if you could implement also `__igetattr__` or
> something, which means:
> 
> instead of
> con = "some text here"
> con  = con.replace("here", "there")
> 
> we could do
> 
> con = "some text here"
> con  .= replace("here", "there")

I have three concerns about this. First, there's a lot of wizardry
under the covers with getattr() and dot access to begin with, so this
would be significantly non-trivial. I suspect the behavior would wind
up being "surprising" too often.

Second, explicit is better than implicit. It's better to explicitly
rebind the name (or mutate on the name) rather than have a fancy
shorthand for trampling over an existing mutable value. In the case of
the compound operators on numbers, you're dealing with immutable types
anyway, so you're really just rebinding the name. The same is true of
your string there. But what about a list?

spam = [1, 2, 3]
eggs = spam
eggs .= append(4)  # what is happening????
eggs .= sort()  # how about now?
eggs .= sorted(eggs) # and now?

You see? It's going to be a lot of surprises, because you're stripping
the usual visual cues of explicit assignment:

spam = [1, 2, 3]
eggs = spam
eggs.append(4)  # mutating spam too
eggs.sort()  # mutating spam too
eggs = eggs.sorted() # rebinding eggs to a new value, spam is safe

Yes, yes, I know, you could "get used to it", but it's adding
complexity and detracting from the One Obvious Way to do things. If an
object decides to implement those compound operators and abuse them
thusly, that's a consenting adults situation. Introducing this new
syntax into the language creates a trip hazard for the user.

Third, that proposed operator, .=  owwwwww that's hard to see. It looks
like a typo, and could easily be typed as one, or overlooked altogether
(again, surprises).

Explicit-is-better-than-implicit'ly yrs,

-- 
Jason C. McDonald
(CodeMouse92)

Author | Speaker | Hacker | Time Lord
-------------- next part --------------
A non-text attachment was scrubbed...
Name: signature.asc
Type: application/pgp-signature
Size: 659 bytes
Desc: This is a digitally signed message part
URL: <http://mail.python.org/pipermail/python-list/attachments/20200930/727fac0d/attachment.sig>


More information about the Python-list mailing list