Today we are going to look at function definitions, variables and global variables

How to define a function

Keyword def + method name + parentheses (arguments)+ colon:

>>> def main(x):
    print("I am a coder");Copy the code

How to understand?

In the main function above, the function definition statement def main(x) is executed when the module containing the function is loaded into the Python interpreter, or if the function is defined in the Python REPL.

The execution of a function definition binds the function name in the current local namespace (the namespace can be thought of as a name-to-value mapping, and this mapping can be nested) to a function object, which is a wrapper around the executable code in the function.

This function object contains a reference to the current global namespace, which is used when the function is called. In addition, the function definition does not execute the function body, only when the function is called.

Method return value
>>> def mudex(nu1,nu2):
     return nu1 * nu2;

>>>mudex(23.5) > > >115Copy the code

Pay attention to

Functions in Python, whether named or anonymous, are collections of statements and expressions. In Python, functions are the first class object, which means there is no limit to how they can be used. Python functions are used just like any other value in Python

Such as strings and numbers. Python functions have properties that can be viewed by using the Python built-in dir function, as shown in the following code:

def square(x):
    return x**2

>>> square
<function square at 0x031AA230>
>>> dir(square)
['__call__', '__class__', '__closure__', '__code__', '__defaults__', '__delattr__', '__dict__', '__doc__', '__format__', '__get__', '__getattribute__', '__globals__', '__hash__', '__init__', '__module__', '__name__', '__new__', '__reduce__', '__reduce_ex__', '__repr__', '__setattr__', '__sizeof__', '__str__', '__subclasshook__', 'func_closure', 'func_code', 'func_defaults', 'func_dict', 'func_doc', 'func_globals', 'func_name']
>>>Copy the code

Some important function properties include the following

1. Doc returns the docstring of the specified function.

2. Name returns the function name.

Func_defaults returns a tuple containing the values of default parameters, which are discussed later.

Func_globals returns a dictionary reference containing the function’s global variables.

def square(x):
    """return square of given number"""
    return x**2

>>> square.func_globals
{'__builtins__': <module '__builtin__' (built-in)>, '__name__': '__main__', 'square': <function square at 0x10f099c08>, '__doc__': None, '__package__': None}Copy the code

Func_dict returns a namespace that supports arbitrary function attributes.

Func_closure returns a cell body tuple containing the binding of the function’s free variables

Variable parameter function

Using “*” is equivalent to Java’s (String.. params)

def getMyStuBy(* params):
    print(params[0]+params[1]+params[2])Copy the code

“**” defines the function

**kwargs

def show_args(arg, *args, **kwargs):
    print arg
    for item in args:
        print args
    for key, value in kwargs:
        print key, value

>>> args = [1, 2, 3, 4]
>>> kwargs = dict(name='testing', age=24, year=2014)
>>> show_args("hey", *args, **kwargs)Copy the code
  • The *args parameter represents the unknown sequence length of positional parameters

  • “**kwargs” represents a dictionary containing keyword and value mappings

  • “Kwargs” can contain any number of keyword and value mappings, and “*args” must precede “kwargs” in the function definition

To read more

Summary of notes for Python beginners

Exploring Python – The basics of the Python language

Let’s have a good time starting Python

My Born-again Brother – a list with shackles

Believe in yourself, there is nothing impossible, only unexpected

Wechat official account: Terminal R&D Department

Technology + Workplace