1

Today I am facing a strange bug about the built in function min(). I noticed that was being rewritten during the execution of my program, so I started to look for in every function of the stack when min() gets overwritten (print(min.__module__)), and I got UnboundLocalError: local variable 'min' referenced before assignment I am still debugging and going back and forth between commits to check what is causing the problem. The program depends on different modules, but the modules are the same as the last time that the program was successful.

I wonder, how is it possible to delete the builtin "min"? I am sure I have not declared any variable with that name, and even if I had, the error would be different. del min should restore the builtin (but in my case I get the error above) builtins['min'] works (in 2 of the 3 systems where I tried)

Any idea of how this is possible?

2 Answers 2

6

In general, the cause of local variable referenced before assignment isn't that someone has deleted min, it's that you've assigned to a variable named min somewhere in your function after the point that you are receiving that error. (The assignment could also be in an if branch that wasn't taken, before the error, but in my experience the first scenario I proposed is more likely.)

Since you have assigned to it somewhere in the function and haven't declared it global, it is a local variable inside that function, and all uses of it refer to that local variable, not to the built-in function. You haven't assigned it a value yet, however, so Python doesn't know what value you want it to have.

Example:

def test():
     x = min(1, 2, 3)                   # error message here
     y = max(1, 2, 3)
     # ... lots of code might go here
     min = x if x < y else y            # local assignment here

test()

The solution is simple. Don't do that. Use a name other than min for your own variable. In fact, it is good practice to avoid using the names of built-ins for your own purposes for this very reason.

Sign up to request clarification or add additional context in comments.

5 Comments

"The solution is simple. Don't do that." A true programmer -- even his answers are supremely reusable! DRY indeed
@kindall Could a third scenario be something like passing min(a,b) as argument to a function (that maybe modifies its arguments) or as value to a dictionary? I am sure I didn't pick up that name, I even searched for it in all the files of the project!
Shouldn't. Posting just the code of the function where you actually get the error would help us pin it down.
I found the offending function, the problem is that it is quite long, and it's the function "at the top" - sort of a script that prepares the data and calls all the other functions. I have already tried to selectively comment portions of it until the error disappeared, but it reappeared when I moved the code to the server with the real data...
Yes, actually I introduced that bug while trying to compensate another bug. min was getting overwritten by another function the takes only one argument, so at some point I put in my code: try: c=min(a,b) except: min = __builtins__['min'] . So the original bug remains, but this question is closed!
4

You can delete it, but you'd have to do it explicitly:

>>> min(5,5)
5
>>> del __builtins__.min
>>> min(5,5)
Traceback (most recent call last):
  File "<stdin>", line 1, in <module>
NameError: name 'min' is not defined

Just deleting it from within a function doesn't remove it from __builtins__, so your callee must be doing it on purpose

2 Comments

sorry, I missed the format in the question. Actually the function is still in builtins, the error is only in one system where I get min = __builtins__['min'] TypeError: 'module' object is not subscriptable
Warning about deleting builtins: If you do del __builtins__.min then you also make the function inaccessible by using explicit import: import builtins; builtins.min(5, 5) -> AttributeError: module 'builtins' has no attribute 'min'. Did you mean: 'bin'?

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.