[Python-ideas] Object grabbing
Robert van Geel
robert at bign.nl
Mon May 2 03:34:31 EDT 2016
On 5/2/2016 5:28 AM, Franklin? Lee wrote:
> On Sun, May 1, 2016 at 4:27 PM, Robert van Geel <robert at bign.nl> wrote:
>> First of all I'm new to this. I tried figuring out if some inquiry like mine
>> below already was posted but I couldn't find it, frankly partly because I
>> don't know what to look for, I'm not sure if there's a name for this idea.
>> I'm not convinced my idea below is solid so apologies if it's naïve but I
>> figured to post it anyway.
>> It has to do with the possibility to fold typical opcodes pairs by
>> introducing a language construct.
>>
>> 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
> Would the following solve your usecase? Explicit naming:
>
> with myobject import a,b,c,d:
> a
> b
> c()
> d = 1
>
>
> Alternatively, putting the object at the end (like in gen expressions):
>
> with a,b,c,d from myobject:
> a
> b
> c()
> d = 1
>
>
> Questions:
> * Should this add additional scope?
> * Descriptors and `__getattr__`-only attributes: Do you get the
> attribute at the start of the block, or do you call `__getattr__`
> every time?
> * That `d = 1` is Pythonically odd if it works as in the original example.
Sidenote: the idea I posed is actually borrowed from a language i used
to use, Visual Foxpro, where it was implemented using the 'with' keyword.
Your proposal has the need to duplicate all variables, so instead of
multiple times typing "self." you have to type them double in the line
using "with" so it's shorter but not optimally short. Also if you want
the accompanying faster opcodes that means you have to put 4 variables
on the stack straight away for fast access, instead of one.
To answer you questions:
- No it would not add scope, it's just a combination of syntactical
sugar combined with faster opcodes.
- The attributes are not grabbed at the beginning of the block, instead
internally in memory there's a 'grabbed object' stack that can be
accessed directly without the LOAD_FAST opcode
- Your fourth remarks refers to your non-dotted code. I think the ".d =
1" statement feels pythonic but again maybe because I was so used to the
syntax in the VFP language
More information about the Python-ideas
mailing list