Python: Is it a bad style to give an argument the same name as a function?

Consider the following code:

def localize(value, localize=None):
    # do something with the localize argument

The variable localizecontains information on whether to comply with the global localization setting or not. It is called by the same name through three layers of code. What lesser evil

  • shadow function name with argument name or
  • use a different name in this function than in the rest of the code base, despite the fact that they have exactly the same meaning?

The function localizedoes not use recursion, so the inability to call itself is not a problem.

/ edit: changing the function name is out of the question since it is an open API. The only room for maneuver is the name of the argument.

+3
4

, . . , locale localization? , , , .

+11

, , , - ? , , .

+1

PEP8, , .

If you are part of a project, jointly specify a naming convention that will not allow you to get confused in the same names and functions. PEP8 suggests adding your variable with an underline in case of a collision with a reserved word, you can do the same in your case.

+1
source

Yes, this is a bad form because it is confusing (2 meanings for one word).

I would rewrite it as a boolean, with True as the default value:

def localize(value, use_global_setting=True):
    ...
+1
source

Source: https://habr.com/ru/post/1771377/


All Articles