Small knowledge, big challenge! This article is part of the “Programmer’s Essentials

This article also participated in the “Digitalstar Project” to win a creative gift package and creative incentive money

If you’re writing Python, you’ve probably used the print function, which we haven’t defined yet.

The answer is that print is a built-in function. Python’s built-in functions are all common utility functions and also built-in functions. Today we will talk about built-in functions

1. What are built-in functions?

Python built-in functions are python functions that do not need to be defined and can be used directly by different built-in functions that have different functions.

#! The/usr/bin/python2.6
# -*- coding: utf-8 -*-
import builtins
def test() :
    print("dsada")
if __name__ == '__main__':
    # add built-in functions
    builtins.__dict__["testFunc"] = test
    testFunc()
    pass
Copy the code

2. What are the built-in built-in functions?

The official documentation links: docs.python.org/3.9/library…

I’ve taken a screenshot of the functions here, so I can give you an overview and guess what they do by looking at their names. Okay

Group the above functions:

Math classes: sum(),pow(),sum(), Round ()

Random classes: Choice (), random(), seed(), shuffle(), Uniform ()

Digital classes: ABS (), min(), Max (), Divmod (), ASCII ()

System class: xxxattr, xxxMethod,

Data type: set (), map (), the tuple (), list (), Boolean (), int (), STR (),

General: Others can fall into this category

3. Description of special functions

3.1 the exec

Format: exec obj

Obj objects can be strings (such as single statements, statement blocks), file objects, or code objects that have been precompiled with Compile.

For example

    exec( "Print (' parsley)")
Copy the code

3.2 the eval

Eval (obj[, globals=globals(), locals=locals()])

Obj can be a string object or a code object that has already been compiled by compile. Globals and locals are optional and represent objects in the global and local namespaces respectively, where globals must be dictionaries and locals is an arbitrary mapping object.

x = 3
print(eval('3*x'))
​
Copy the code

The output is 9, and Eval can refer to context

3.3 the compile

Format: compile(STR, file, type)

Compile statements create code objects from statements in STR from types of type (including ‘eval’ : used with eval, ‘single’ : used with exec of a single statement, and ‘exec’ : used with exec of multiple statements). File is where the code is stored, usually “.

The purpose of the compile statement is to provide a one-time bytecode compilation that does not need to be recompiled on each subsequent call.

The compile() function compiles strings into code objects, which are then executed by exec statements and can then be evaluated using the eval() function. The filename argument should be the filename from which the code reads. If the filename is generated internally, the filename parameter value should be the corresponding identifier. The kind argument specifies the class of code contained in the string argument

3.4 globalsAnd locals

Globals () returns all global variables of the current location as dictionary types.

Locals () returns all local variables of the current location as a dictionary.

It returns True for functions, methods, lambda functions, classes, and class instances that implement the Call method.

Add a built-in function of your own

Some function is often called the development process, but each time it can be a trouble to guide package or something, you think can put a few tools function added to the built-in function, as long as there is a problem, someone else will have the same problem, in fact, the solution is simple, is to add in the builtins dict function is ok, here is an example:

#! /usr/bin/env python
# encoding: utf-8"""
#Author: 香菜
@time: 2021/10/10 0010 下午 10:22
"""
import builtins
def test() :
    print("dsada")
if __name__ == '__main__':
    # add built-in functions
    builtins.__dict__["testFunc"] = test
    testFunc()
    passCopy the code

5, summary

There is nothing special about python’s built-in functions, just some tools and methods provided by the system, and they are easy to implement. Remember these common tools and methods, and you can get twice the result with half the effort in development.