[Date Prev][Date Next][Thread Prev][Thread Next][Date Index][Thread Index]

Ways to make a free variable local to a function?

On Tue, Mar 6, 2018 at 12:52 AM, Terry Reedy <tjreedy at udel.edu> wrote:
> On 3/5/2018 7:12 AM, Kirill Balunov wrote:
>> # 1. By passing through local variable's default values
>>      def func_local_1(numb, _int = int, _float = float, _range = range):
> You are not required to mangle the names.
> def func_local_1(numb, int = int, float = float, range = range):
> ...

Even so, this does mess up the function's signature, leaving your
callers wondering if they can call it with some sort of range
parameter. (Though in this particular instance, range() is only called
once, so it's pretty much useless to try to optimize it.)

In theory, the CPython bytecode compiler (don't know about other
Python implementations) could just add these as constants. They'd then
be bound at either compile time or function definition time (by
default the former, I think, but the latter would be more useful), and
be looked up as quickly as locals. I'm not sure how useful this would
be, though.

If PEP 572 [1] were to be accepted, you could do something like this:

def func(numb):
    if ((int as int), (float as float)):
        res = []
        for i in range(numb):
            res.append(int(i) + float(i))
        return res

Syntactically a bit clunky, but keeps everything inside the function,
and DOES create local variables. Not sure it's better than your other
options, but it is another option.

[1] PEP 572: https://www.python.org/dev/peps/pep-0572/