The definition and use of the function of Python tutorial reading

Source: Internet
Author: User

Definition of a function

Python uses the keyword def definition function, which is similar in format to C, but does not have a return type, and the parameter does not require a set type.

def add(a, b):    """这是函数的文档字符串"""    return a + bdef append(data):    data.append(a) #如果data为可变对象时候,例如List、Dict,则会改变实参的值    

A function call generates a new symbol table for the function local variable. To be exact, the assignment of variables in all functions is to store the values in the local symbol table. The variable reference is first found in the local symbol table, followed by the local symbol table containing the function, then the global symbol table, and finally the built-in name table. Therefore, global variables cannot be assigned directly in the function (unless named with the global statement), although they can be referenced.

Python parameter passing is definitely a way to "pass an object reference". This approach is equivalent to a synthesis of the value of the pass and the reference. If a function receives a reference to a mutable object (such as a dictionary or a list), it can modify the original value of the object-the equivalent of passing the object through a "pass reference". If a function receives a reference to an immutable object (such as a number, character, or tuple), it cannot directly modify the original object-the equivalent of passing the object through a "pass value".


Parameters

Default parameter: Sets the default value for the parameter, and when the function is called, the default value is used if there is no value.

def login(username,password = "123"):    """登录函数"""    passlogin("admin")login("admin","password")def f(a, L=[]):    L.append(a)    return Lprint(f(1))print(f(2))print(f(3))# 输出结果[1][1, 2][1, 2, 3]>>> def f(x = []):...     x.append(1)...     

Keyword parameter invocation: that is, specifying the form key = value to invoke the function, the keyword argument should be behind the position parameter.

def parrot(voltage, state=‘a stiff‘, action=‘voom‘, type=‘Norwegian Blue‘):    print("-- This parrot wouldn‘t", action, end=‘ ‘)    print("if you put", voltage, "volts through it.")    print("-- Lovely plumage, the", type)    print("-- It‘s", state, "!")# 以下调用均是错误的parrot()                     # 必须参数没有parrot(voltage=5.0, ‘dead‘)  # 关键字参数必须在位置参数之后parrot(110, voltage=220)     # **任何参数都不可以多次赋值**parrot(actor=‘John Cleese‘)  # 不存在该参数名

The definition and use of the function of Python tutorial reading

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.