[Python-ideas] Object grabbing
Steven D'Aprano
steve at pearwood.info
Sun May 1 21:02:11 EDT 2016
Hello Robert, and welcome,
On Sun, May 01, 2016 at 10:27:04PM +0200, Robert van Geel wrote:
[...]
> The idea is to be able to write this code:
>
> myobject.a
> myobject.b
> myobject.c()
> myobject.d = 1
>
> like this:
>
> using myobject:
> .a
> .b
> .c()
> .d = 1
I think this is closely related to the Pascal "with" statement, which has a FAQ:
https://docs.python.org/2/faq/design.html#why-doesn-t-python-have-a-with-statement-for-attribute-assignments
You require a leading dot to access attribute names, which avoids the
ambiguity between attributes and variables, but it also fails the "new
syntax shouldn't look like grit on Tim's monitor" test.
using myobject:
.method(.eggs + (.spam or ham) - tomato - .cheese)
All of these proposals raise the question, what do you do with nested
blocks?
using myobject:
print(.attr)
using yourobject:
print(.attr)
Obviously in the outer block, ".attr" refers to myobject. My guess is
that inside the inner block, it refers to yourobject. What happens if
you want to refer to both? Would we have to write this?
using myobject:
print(.attr) # myobject
using yourobject:
print(.attr) # yourobject
print(myobject.attr) # be explicit
Now imagine that you are a beginner, trying to understand this code
block. What would you think it does? Might you not be surprised that
the two references to ".attr" refer to different variables?
And of course there is always the objection that the barrier to adding a
new keyword is quite high. Somewhere out there, somebody is using
"using" as a variable name (perhaps a decorator?) and making this a
keyword will break her code. Is it worth it?
Despite these objections, I'm cautiously interested in this. I don't
think the objections are insurmountable, and if there is a significant
gain in readability and performance, it may be worth while.
A cautious and tentative +1.
It may be worth you doing a survey of other languages and seeing if they
have anything similar.
--
Steve
More information about the Python-ideas
mailing list