[Python-ideas] Python 3.9.9 - The 'I Have A Dream' Version

Chris Angelico rosuav at gmail.com
Wed Apr 15 16:53:00 CEST 2015


On Thu, Apr 16, 2015 at 12:39 AM, Simon Kennedy <sffjunkie at gmail.com> wrote:
> On Wednesday, 15 April 2015 15:08:57 UTC+1, Chris Angelico wrote:
>>
>> > This is probably what I should have used "First class values"
>>
>> That's still pretty much everything in Python.
>
>
> Except for statements such as 'if' / 'for' or operators.

I say below "manipulable". You can't in any way manipulate an 'if'
statement; however, you can play around with the compiled byte-code,
which is represented either by a bytestring or a code object,
depending on your point of view.

>> Off the top of my head,
>> the only manipulable things that aren't first-class are name bindings
>> themselves, and those can usually be simulated - most namespaces are
>> representable as dictionaries, and other bindings are
>> __[gs]et{item,attr}__ calls; the notable exception is function locals,
>> which you can't easily mutate from outside the function.
>>
>
> Why would you want to mutate function locals from outside the function?
> That's not something I've ever explored.

In C, you can do it with pointers:

void swap(int *x, int *y) {
    int tmp=*x;
    *x = *y;
    *y = tmp;
}

void func() {
    int foo, bar;
    swap(&foo, &bar);
}


In Python, you can pass locals() to allow another function to _read_
your locals, but not change them.

ChrisA


More information about the Python-ideas mailing list