Python inner function parameter shadowed

Brendan Abel 007brendan at gmail.com
Tue Sep 13 14:28:38 EDT 2016


This looks like a decorator function that optionally accepts arguments to
change the behavior.

You can safely ignore the warning form PyCharm.  the variable won't be
shadowed it's included in the function signature of the inner function.

A lot of times, the outside decorator will just use the *args or **kwargs
parameters and check whether the first arg is callable or not and if any
keywords have been passed in to decide which version of the wrapped
function to return

Here is an example:
http://stackoverflow.com/questions/653368/how-to-create-a-python-decorator-that-can-be-used-either-with-or-without-paramet

On Tue, Sep 13, 2016 at 9:40 AM, Chris Angelico <rosuav at gmail.com> wrote:

> 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
> --
> https://mail.python.org/mailman/listinfo/python-list
>



More information about the Python-list mailing list