Python inner function parameter shadowed

Chris Angelico rosuav at gmail.com
Tue Sep 13 12:40:25 EDT 2016


On Wed, Sep 14, 2016 at 2:34 AM, Daiyue Weng <daiyueweng at gmail.com> wrote:
> Hi, I am using inner function like this,
>
> def timeit(func=None, loops=1, verbose=False):
>     if func is not None:
>         def inner(*args, **kwargs):
>
>             # some code
>
>         return inner
>     else:
>         def partial_inner(func):
>             return timeit(func, loops, verbose)
>
>         return partial_inner
>
>
> PyCharm warns about "Shadows name 'func' from outer scope" on
>
> def partial_inner(func):
>
> How to fix it?
>
> cheers

Hmm. I think the real solution here is to separate this into two functions:

def timeit(func, loops=1, verbose=False):
    def inner(*args, **kwargs):

        # some code

    return inner

def timeit_nofunc(loops=1, verbose=False):
    def partial_inner(func):
        return timeit(func, loops, verbose)
    return partial_inner

But without knowing more about what you're accomplishing, I can't
really say. If you just want a quick fix to shut the linter up (note
that this is NOT a Python error, it's just a message from a linter),
you could use a different name for one of the variables.

ChrisA



More information about the Python-list mailing list