0

I got a python error that surprised me this morning. Consider this simple example:

def f(mylist):
    m = max(mylist)
    max = m

I expected this function to execute without failing, but it throws:

UnboundLocalError: local variable 'max' referenced before assignment

I assumed that since python was an interpreted language, overwriting the built-in max() function wouldn't cause any trouble as long as I didn't try to invoke it again afterwards.

Why does this behavior occur? Does the python interpreter "look ahead" for variable declarations?

PotatoBoy
  • 45
  • 6

0 Answers0