What are built-in functions? And what is your difference from reserved words?

Asked

Viewed 3,970 times

5

A little while ago I started to study the Python language and in the middle of studies a question arose. What are functions built-in? And what is your difference of reserved words? I was confused by this question, because to me it seems to be the same. Am I right? I apologize if you’re talking nonsense.

1 answer

5


Very good question - let’s put it this way:

Python seeks to be an elegant language. In this sense, the key words of the language are what enable its mechanics: conditional with "if", "Elif" and "Else", loop structures with "for" and "while", definition of functions with "def" and types with "class", association of names with objects (the same effect as "assigning values to variables") with the operator "=" and so on.

These effects, having data to work with, allow you to create any program - but note the following: none of them "get" data from any form of input, nor write data anywhere. These are the mechanisms of language itself. (In version "2" of Python there was an exception that was the "print" command, a reserved word that had the side effect of putting data in the output - either in the terminal, or in a file) - That is, all the essential "logic" of the language is in the keywords (even if they don’t look like keywords, like the operator "=").

Now, any program, to have any effect on the environment in which it is running, needs to interact with the system in some way - be it reading and writing in files, on the screen - and it is also interesting to have other utilities like knowing the length of sequences, create sequences and new types - in short, things that are more or less universal tools for the program to be able to "do something". The Python people realized throughout the language evolution itself that having these tools readily available, without having to import any library or module, made it much easier to perform complete tasks. But all these communications are made, at some level with the operating system, and can be encapsulated in a function - which for those who are writing a program, is identical in importance to any function that the programmer himself creates.

Then came these functions that are always available, even if you don’t mind any package or module from the standard library: the print (in Python 3, it passes to function status), input, open to open a file, len, max, min, type, range among others. These are some of the built-in functions - and the only difference from them to any other function is that they are automatically available in any module or context.

The complete list of them is here: https://docs.python.org/3/library/functions.html

The existence of these built-in functions (and other variables) is what makes it possible in Python to have a program that opens a file for writing and writes some content to it in a single line - while in Java, for example, the same operation - which is one of the most fundamental things for a program: interacting with the environment - this requires importing two distinct classes of modules, and creating one instance of them - just to start interacting with the file - and the documentation to find these classes is even trivial to find. In Python this requires only open("meuarq.txt").write("Alô mundo!") - the open is a built-in.

Now, the story doesn’t end there: Python has a very interesting and consistent design of searching for function and variable names - which gives the programmer complete control over what "exists" in a given scope of the program. The complete mechanism is more or less this: when finding a function name (or other object), the Python Runtime first checks whether that name exists in the local variables (within the running function, for example). If it does not exist, it searches for "global" variables - which are global in a module - in general names of functions and classes defined by the programmer are in this scope. If the name is not found in either of these two places, Python searches the module __builtins__ - where all these functions are defined.

This allows, for example, that for testing, play, or even temporary modification of the behavior of a real function, you change the value of a "built-in" function by simply creating another function with the same name. The function will be created as a global or local variable, and can be used normally - until it is deleted, for example, with the command "del" - at that point, the original definition in __builtins__ reuse.

This allows for example writing code that works at the same time in Python 2.7 and 3.X, redefining at the beginning of the module the name "input" to be "raw_input" if we are in Python 2 - then your program can use "input" and work normally in Python 2 - but already using the modern name of the function:

from __future__ import print_function
import sys

if sys.version[0] < 3:
    input = raw_input

# Restante do programa

(Since Python print 2 is a command, the special syntax from __future__ import print_function is required to bring print as a function, equivalent to Python 3, to a Python 2 script. But the simple assignment input = raw_input exchange the input with Python 2 "Eval" for the "raw_input" that always reads a string, such as input python 3.

(And, yes, it is also possible to change the meaning of a function "builtin" by directly changing the name in __builtins__ - this will make the replacement in all modules of the program - but then you lose control of the situation - it is not recommended to do it)

  • Sensational guy your very good explanation! Thank you clarified my doubts.

Browser other questions tagged

You are not signed in. Login or sign up in order to post.