Software Testing/Test Development丨Study Notes Python Functions

python function

What the function does

  • Functions are organized, reusable code segments used to implement a single or related function
  • Functions improve application modularity and code reuse
  • Python built-in functions: docs.python.org/zh-cn/3.8/l…

function definition

  • def: function definition keyword
  • function_name: function name
  • (): The location where the parameter list is placed, which can be empty
  • parameter_list: optional, specifies the parameters passed to the function
  • comments: optional, specify comments for the function
  • function_body: optional, specify the function body
def function_name([parameter_list]):
    [''' comments ''']
    [function_body]

Things to note when defining functions

  • Indentation: python judges code blocks through strict indentation

    • The function body and comments must maintain a certain indentation relative to the def keyword, usually 4 spaces.
    • pycharm automatic formatting shortcut keys:ctrl+alt+L
  • Define empty function

    • Use  pass statement placeholder
    • Write function comments comments

function call

  • function_name: function name
  • parameter_value: optional, specify the value of each parameter
function_name([parameter_value])

Parameter passing

  • Formal parameters: When defining a function, the parameters in parentheses after the function name
  • Actual parameters: When calling a function, the parameters in parentheses after the function name
# a, b, c 为形式参数
def demo_func(a, b, c):
    print(a, b, c)

# 1, 2, 3 为实际参数
demo_func(1, 2, 3)

Positional parameters

  • The quantity must be the same as when defined
  • The location must be the same as when defined
def demo_func(a, b, c):
    print(a, b, c)

# 1 赋值给 a, 2 赋值给 b, 3 赋值给 c
demo_func(1, 2, 3)

keyword arguments

  • Use formal parameter names to determine input parameter values
  • Does not need to exactly match the position of the formal parameters
def demo_func(a, b, c):
    print(a, b, c)

demo_func(a=1, b=2, c=3)

Set default values ​​for parameters

  • You can specify default values ​​for formal parameters when defining a function
  • Formal parameters that specify default values ​​must be placed at the end of all parameters, otherwise a syntax error will occur.
  • param=default_value: Optional, specify the parameter and set the default value for the parameter to default_value
def function_name(..., [param=default_value]):
    [function_body]

function return value

  • value: optional, specifies the value to be returned
def function_name([parameter_list]):
    [''' comments ''']
    [function_body]
    return [value]

Python function advanced and parameter processing

variable parameter

  • Variable parameters are also called variable length parameters

  • The actual parameters passed into the function can be any number

  • common forms

    • *args
    • **kwargs

*args

  • Receives any number of actual arguments and puts them into a tuple
  • To use an existing list or tuple as a variable parameter of a function, you can add*
def print_language(*args):
    print(args)

print_language("python", "java", "php", "go")

params = ["python", "java", "php", "go"]
print_language(*params)

**quargs

  • Receives any number of actual parameters that are explicitly assigned like keyword parameters and puts them into a dictionary
  • To use an existing dictionary as a variable parameter of a function, you can add**
def print_info(**kwargs):
    print(kwargs)

print_info(Tom=18, Jim=20, Lily=12)

params = {'Tom':18, 'Jim':20, 'Lily':12}
print_info(**params)

python lambda expression

anonymous function

  • function without name
  • Create anonymous functions using lambda expressions

scenes to be used

  • Need a function, but don’t want to bother naming it?
  • Usually in scenarios where this function is only used once
  • Short callback functions can be specified

grammar

  • result: call lambda expression
  • [arg1 [, arg2, …. , argn]]: optional, specifies the parameter list to be passed
  • expression: required, specify an expression to implement a specific function
result = lambda [arg1 [, arg2, .... , argn]]: expression
book_info = [
    ("python", 80),
    ("java", 70),
    ("ruby", 90),
]
book_info_sorted = sorted(book_info, key=lambda x: x[1])
print('sorted(book_info) returns:', book_info_sorted)

Finally, I would like to thank everyone who reads my article carefully. Reciprocity is always necessary. Although it is not a very valuable thing, if you can use it, you can take it directly:

This information should be the most comprehensive and complete preparation warehouse for [software testing] friends. This warehouse has also accompanied tens of thousands of test engineers through the most difficult journey. I hope it can also help you!

Guess you like

Origin blog.csdn.net/YLF123456789000/article/details/135294832