[Python-ideas] Object grabbing

Guido van Rossum guido at python.org
Sun May 1 23:27:40 EDT 2016

On Sun, May 1, 2016 at 7:05 PM, Chris Angelico <rosuav at gmail.com> wrote:

> On Mon, May 2, 2016 at 11:36 AM, Steven D'Aprano <steve at pearwood.info>
> wrote:
> > On Sun, May 01, 2016 at 09:25:17PM -0400, Random832 wrote:
> >
> >> Is there any reason not to simply implement a performance optimization
> >> for the normal syntax?

If the variable is already a local, there's not much of a performance win
to be had, since replacing a LOAD_FAST with a DUP_TOP doesn't make much of
a difference (and if it is anything else the optimization isn't safe, as
you already explained).

HOWEVER... I doubt that this is what the OP is after. I also don't think
they're really after having to write less. I think what they want is to
express the IDEA of doing various things to/using attributes of a specific
object. (OK, I will now stop using all caps. :-)

Like Steven, I think it's a reasonable idea.

The idea of using a leading dot is good. There are some edge cases around
nesting but I think they can be dealt with.

I have also seen plenty of code that would benefit from this idiom, e.g.
code that repeats something like `team.users[i]` several times in one call
-- I imagine the author just didn't want to interrupt their flow by putting
it in a local variable.

But unlike Steven, I'm still lukewarm at most and would currently vote -0.
Does this feature find a lot of use in other languages? If so, is the code
using it typically clearer than the alternative?

If a coder currently writes

team.evict_user(team.users[i].uid, team.users[i].email, "Bye bye")

because they can't be bothered to write

user = team.users[i]
team.evict_user(user.uid, user.email, "Bye bye")

then would they bother writing this instead?

using team.users[i]:
    team.evict_user(.uid, .email, "Bye bye")

And is that really clearer? Even if it's used often enough that people will
recognize it, the dot is an awfully small character and easily missed (on
my screen there are smudges larger than a dot in the standard font :-).

Plus there's the cost of the extra indent. Sure, omitting the user variable
probably makes the line shorter, but what if there are other lines in the
same block that don't use it? I've seen plenty of code doing something less
readable to avoid an extra indent (which might require breaking lines that
would otherwise just fit).

--Guido van Rossum (python.org/~guido)
-------------- next part --------------
An HTML attachment was scrubbed...
URL: <http://mail.python.org/pipermail/python-ideas/attachments/20160501/1c9bfd09/attachment.html>

More information about the Python-ideas mailing list