1
def print_name(your_name = "Anonymous Andy"):
    print("Hello, " + your_name)

def your_name():
    # This function has same name as variable in above function
    return None

print_name() # Prints "Hello, Anonymous Andy"
print_name("World") # Prints "Hello, World"

Above code is written for academic purpose to learn Python, and not for production. Pylint (2.14.0) warns me:

1:15: W0621: Redefining name 'your_name' from outer scope (line 4) (redefined-outer-name)

Question

  1. I assume Pylint is warning me "Hey you are redefining the your_name function". Is this understanding correct?
  2. If above understanding is correct then I think Pylint is wrong, and I think I am redefining the your_name variable which is local to the print_name function's scope. Is this understanding correct?
  3. If my above understandings are correct, I am further puzzled because I don't think a famous program like Pylint would raise such false alarm. The fact that it raised an alarm is not wrong, but the message looks like a lie because I'm not redefining anything from outer scope. Am I missing something?
OneCricketeer
  • 179,855
  • 19
  • 132
  • 245
dungarian
  • 203
  • 2
  • 8
  • 1
    because the argument `your_name` is hiding the function with the same name – DeepSpace Jun 06 '22 at 16:07
  • 6
    Your understanding is correct that you're only redefining the variable within the scope of `print_name`, but this is not a "false alarm". Imagine how confused you'd be if you tried to call the `your_name` function from inside `print_name`, not realizing that you'd carelessly redefined `your_name` to something else within that scope. "Why does it work in all my other functions but give me a `TypeError` inside this one???" Linter warnings are there to help you avoid forehead-smacking moments like that. – Samwise Jun 06 '22 at 16:08
  • @Samwise Thanks for validating my understanding. If I **try** to call the `your_name` function from inside `print_name`, Python will complain "`your_name` is a string, and you can't call it" with a straight face. So if Pylint was to warn me (which is helpful), shouldn't the message be something like "You're using a confusing/existing name"? – dungarian Jun 06 '22 at 16:23
  • 4
    "Redefining name" means the same thing as "you're using an existing name". Pylint errors tend toward brevity since you sometimes find yourself scrolling through a lot of them. They usually take a bit of puzzling the first time you see them (like you did here) but they soon become like old friends. – Samwise Jun 06 '22 at 16:35

1 Answers1

3
  1. Yes

  2. No. Functions are interpreted before their parameters, and within the module, are defined in the "outer scope". Your code would be interpreted the same as this; therefore, the error would make more sense, visually.

def your_name():
    return None

def print_name(your_name = "Anonymous Andy"):
    print("Hello, " + your_name)

If you were to redefine a local variable, that would look like this

def print_name(your_name = "Anonymous Andy"):
    your_name = ""
    print("Hello, " + your_name)
OneCricketeer
  • 179,855
  • 19
  • 132
  • 245