2

Python includes the built in max() function. However, despite it being built in it is not a keyword. That is to say, you are allowed to do max=4. This makes sense since the maximum of something comes up a lot. But! If you use max as a variable, then it disables use of the max function in that scope.

So if you do:

max = 4
max(1, 2)

You will get an error of int object not callable. Again, makes sense. But is there any way to specify that you would like the max function? Like a std.max()? This goes for all other built in functions as well.

9
  • 4
    Do not name your variables after Python builtins, period. Commented Feb 18, 2016 at 0:16
  • 1
    @Will, I understand what you're saying but in terms of readability the names min and max are great. Thus there are times where you might create internal naming conflicts so that your API is user friendly. Commented Feb 18, 2016 at 0:21
  • It would be better to use a more descriptive name. For instance, if you have a list called things, use max_thing. Commented Feb 18, 2016 at 0:31
  • @NickChapman: If you had a max function, for example, if you were NumPy and you had numpy.max, it'd make sense to use the same name as a builtin. For max = 4? No way that's part of your API. Call it max_ or something. Commented Feb 18, 2016 at 0:44
  • 1
    @NickChapman Yeah, I understand. I've wanted to name variables class or max or file before, too, but every language has its sacrifices you have to make. I personally use OOP-conventions for all of my names (unless I'm working on other people's code), so for me, it'd be maxValue or maxThingName. It's always good to be descriptive. If it's just a throwaway variable that doesn't need to be descriptive, call it m? I really, really, really wouldn't name things after builtins. Commented Feb 18, 2016 at 2:56

1 Answer 1

8

The __builtin__ (Python 2) / builtins (Python 3) module provides another way to access all built-in/standard identifiers for cases like this:

>>> import __builtin__
>>>
>>> __builtin__.max is max
True
>>>
>>> max = 2
>>> __builtin__.max([0, max])
2
import __builtin__ as builtins

def random_integer(min, max):
    random_integer.seed = builtins.max(10101, ( # looks random enough, right?
        ((random_integer.seed * 3 - 210) % 9898989) >> 1) ^ 173510713571)
    return min + (random_integer.seed % (max - min + 1))

random_integer.seed = 123456789

This module is not normally accessed explicitly by most applications, but can be useful in modules that provide objects with the same name as a built-in value, but in which the built-in of that name is also needed.

The name change in Python 3 is part of the "core languages" changes described in PEP 3100:

In order to get rid of the confusion between __builtin__ and __builtins__, it was decided to rename __builtin__ (the module) to builtins, and to leave __builtins__ (the sandbox hook) alone.

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

2 Comments

The name of the module changed between Python 2 and Python 3? Interesting.
Then you can do __builtins__.max = 2 and really cause trouble!

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.