• python_形参、实参


    #参数:形参、实参
    '''
    def display_message(title):

    print("My favourite book is %s" %title)

    #return 0


    #display_message('alice in Wonderland'.title())

    def describe_pet(pet_name,animal_type="dog"):
    """显示宠物信息"""
    print("My "+animal_type+"'s name is "+pet_name)

    describe_pet(pet_name="Who") #该处实参使用形参的默认值,animal_type = dog
    describe_pet(pet_name="Who",animal_type="cat") #animal_type提供了实参,Python将忽略形参的默认值

    #describe_pet() #实参不匹配,没有指定任何参数,实参个数必须与形参个数一致

    def null(x,y):
    print(x)
    print(y)

    null(1,2)
    print(display_message("mo"))

    '''

    #返回值
    #让返回值变为可选的,只需要把可选的形参放在最后,并指定一个默认值——空字符串。但是在传实参时,位置与形参一一对应。
    '''
    def full_name(first_name,last_name,middle_name =''):
    full_name = first_name+' '+middle_name+' '+last_name

    return full_name.title()
    name= full_name("liu","qian",'01')
    print(name)
    '''

    #返回字典
    '''
    def info(name,age ,sex):
    """返回一个字典,其中包含有关一个人的信息"""
    message = {'name':name,'age':age,'sex':sex}
    return message

    b=info("Monica",24,"F")
    print(b)

    def test1(**kwargs):
    print(kwargs)

    test1(name = "Lee",age = 24)
    '''
    #结合函数和while循环
    def full_name(first_name,last_name):
    full_name = first_name+' '+last_name
    return full_name.title()

    exit_flag = False
    while not exit_flag:
    print("Please input your name:"," ","(enter 'q' at any time to quit)")
    f_name = input("First_name:") #输入first_name,last_name
    if f_name == 'q':
    #exit_flag = True
    break

    l_name = input("Last_name:")
    if l_name == 'q':
    exit_flag == 'q'
    name = full_name(f_name,l_name) #调用方法传入实参
    print(name)
    ----------------------------------------------------
    def data(host,port=3396):
    pass

    data()
    '''
    #默认参数特点:调用函数时,默认参数非必传
    #用途:1.默认安装值 2.数据库的端口号


    '''
    def test(**kwargs):
    print(kwargs)
    print(kwargs['name'])

    test(name = 'Alex',age =22)
    test(**{'name':'Alex','age':22})
    '''


    #*args接收N个位置参数,不能接收关键字参数,转换成元组的形式。
    #kwargs把N个关键字参数,转换成字典的方式
    def test2(name,hobby='tesla',*args,**kwargs):
    info = {}
    info['name']=name
    info['hobby']=hobby
    for key,value in kwargs.items():
    info[key]=value
    # print(name)
    # print(kwargs)
    return info

    user_info =test2('alex','blue','apple',age=23,sex="f")
    print(user_info)

  • 相关阅读:
    洛谷 P1786 帮贡排序 题解
    Bayes++ Library入门学习之熟悉UKF相关类
    Bayes++ Library入门学习之熟悉class-Bayesian_filter_base(2)
    Bayes++ Library入门学习之熟悉class-Importance_resampler
    Bayes++ Library入门学习之熟悉class-Bayesian_filter_base(1)
    Bayes++ Library入门学习之熟悉namespace
    CMake入门之创建一个基于PCL的最小工程
    CUDA学习之从CPU架构说起
    #pragma 预处理指令详解
    C++中inline函数
  • 原文地址:https://www.cnblogs.com/monica001/p/10490712.html
Copyright © 2020-2023  润新知