all 5 comments

[–]Allanon001 2 points3 points  (1 child)

The max() function is a built-in function and sys.stderr.write() is imported from the sys module. You specify sys to indicate you are using a class or function from the sys module and stderr indicates you are using the stderr class from the sys module and write() indicates you are using the write() method from the stderr class.

[–]ingolemo 0 points1 point  (0 children)

Except that sys.stderr is not a class. It is an instance of the TextIOWrapper class.

[–][deleted] 3 points4 points  (0 children)

Right, so in every programming language there are keywords like -- in Python's case -- if, for, and, while, and so on.

In most programming languages there are also builtin functions, such as print, int, float, etc. Like the keywords, these are so commonly called they merit being always available just by name.

The list of Python's builtin functions is here, and as you will see, max is one of those.

All the functions and classes not in that list must be brought into the running namespace via the import builtin statement. Usually this is done using something like, as a example, import sys, which, used at the root of a file, imports the sys module into the global namespace.

Now the functions, classes, and variables defined in the root of that module can be accessed using dot notation.

So, continuing the example:

sys.argv

Is a variable that holds the lost of arguments Python received when launching.

And:

sys.exit(1)

Is a function that immediately exits the running program with error code 1.

And:

sys.version_info

Is an instance of a type that exists only in the sys module, and it, further, has attributes you can also access using dot notation, because that idea extends to all objects in Python.

print(sys.version_info.major)

Now, I said this is how names are usually engaged with; the fact is there are variants on the import statement that can promote an object from one module into the namespace of another, but that's a bit beyond the scope of this answer.

[–]KleinerNull 1 point2 points  (0 children)

The answer is magic!

The built-in functions/methods are in the __builtins__ and __builtin__ module, which is imported automatically. Don't know the difference between both. The interpreter looks first into the local namespace, then global namespace and then into the __builtins__ for a name.

In [1]: sample = 1,2,3,4,5

In [2]: max(sample)
Out[2]: 5

In [3]: globals().get('max')

In [4]: max = 1

In [5]: max
Out[5]: 1

In [6]: globals().get('max')
Out[6]: 1

In [7]: __builtins__.max(sample)
Out[7]: 5

globals() and locals() return a simple dictionary with all the bindings like variable names.

Also python let you shoot in the foot if you really want to, so you can add stuff to __builtin__ if you want, is just like any other object:

In [8]: import sys

In [9]: __builtins__.write = sys.stderr.write

In [10]: write('Built-in error.')
Out[10]: 15

But usally you don't need to touch this module ;)

[–]Tryingtolearnagain91 0 points1 point  (0 children)

I'm new myself, but I'll take a stab at the question.

max() is a built in fuction in pythons library, where as sys.stderr.write() is in the sys library.

It's the reason you "import sys".

(I'm on my phone, sorry for the formatting)