Python introductory notes--from function to magic method


(unfinished, catalog)

function

function definition

A function defbegins with a keyword, followed by the function name and parentheses ().
The code executed by a function begins with a colon and is indented.
return[expression] Terminates the function, optionally returning a value to the caller. returnEquivalent to return without expressionNone

function call

function documentation

function parameters

  • positional arguments
  • default argument
  • Keyword arguments
  • Named keyword arguments (name keyword argument)

parameter combination

  • To define a function in Python, you can use positional parameters, default parameters, variable parameters, named keyword parameters, and keyword parameters. Four of these five types of parameters can be used together, but note that the order of parameter definition must be:

Positional arguments, default arguments, variadic arguments, and keyword arguments.
Positional arguments, default arguments, named keyword arguments, and keyword arguments.

  • Note the syntax for defining variadic and keyword arguments:

*argsIs a variable parameter, argswhat is received tuple
**kwis keyword parameter, kwwhat is received is adict

Named keyword parameters are used to limit the parameter names that the caller can pass in, while providing default values.
Don't forget to write the delimiter when defining the named keyword parameter *, otherwise the definition is a positional parameter.

  • Note: Although you can combine up to 5 parameters, don't use too many combinations at the same time, otherwise the function will be difficult to understand.

variable scope

  • In Python, program variables are not accessible anywhere, and access rights depend on where the variable is assigned.

  • Variables defined inside a function have a local scope and are called local variables.

  • Variables defined outside a function have global scope and are called global variables.

  • Local variables can only be accessed within the function in which they are declared, while global variables can be accessed throughout the program.

Lambda-expression

Definition of anonymous function

  • There are two types of functions in Python:
    the first type: regular functions defined with the def keyword;
    the second type: anonymous functions defined with the lambda keyword

Python uses the lambda keyword to create anonymous functions instead of the def keyword. It has no function name and its grammatical structure is as follows:
lambda argument_list: expression

  • lambda - Keyword for defining anonymous functions.
  • argument_list - function arguments, they can be positional arguments, default arguments, keyword arguments, same as arguments in regular functions.
  • :- Colon, add a colon between function parameters and expressions.
  • expression - just an expression that takes function arguments and outputs some value.

It should be noted that
expressionthere is no returnstatement , because lambdadoes not need it to return, and the result of the expression itself is the return value.
An anonymous function has its own namespace and cannot access parameters outside its own parameter list or in the global namespace.

Application of anonymous functions

non-functional programming

def f(x):
    for i in range(0, len(x)):
        x[i] += 10
    return x


x = [1, 2, 3]
f(x)
print(x)
# [11, 12, 13]

functional programming

def f(x):
    y = []
    for item in x:
        y.append(item + 10)
    return y


x = [1, 2, 3]
f(x)
print(x)
# [1, 2, 3]

classes and objects

object = property + method

We can classdefine Pythona class using the keyword followed by the name of the class, a semicolon, and the implementation of the class.

  • Inheritance: A mechanism for subclasses to automatically share data and methods between parent classes

what is self?

Python's selfis equivalent to C++'s thispointer.

Python's magic methods

If the object implements one of the following magic methods, then this method will be called by Python under special circumstances, which happens automatically

. . . . . . incomplete

Guess you like

Origin blog.csdn.net/wangzaiyouzr/article/details/113600073