#!/usr/bin/env python
# -*- coding:utf-8 -*-
# 返回值

def f1():
    return 'sds'


print(f1())


# 返回列表
def f1():
    return ['sds', 'dsfrsdf', 1313]


print(f1())


# 返回元组
def f1():
    return 3, 4, 5, '2222', 'rewrew'


print(f1())


# 多个返回值则返回元组
def f1():
    return 3, 4, 5, ['sds', 'dsfrsdf', 1313]


print(f1())


# 返回元组
def f1():
    return (3, 4, 5, ['sds', 'dsfrsdf', 1313])


print(f1())


# 单个返回值,返回原类型
def f1():
    return {'name': 'yonge', 'age': 20}


print(f1())


# 传参
def f1(x, *args):
    print(args)  # (3,4,5,6)


f1(1, 3, 4, 5, 6)


def f1(x, *args):
    print(args)  # ([3, 4, 5, 6],) 原样输出

# 传字典
f1(1, [3, 4, 5, 6])
def f1(x, *args):
    print(args)  # (3, 4, 5, 6) #迭代输出

f1(1, *[3, 4, 5, 6])

#传key=value
def f1(x, **kwargs):
    print(kwargs)  # {'a': 1, 'b': 2, 'c': 3}#转成字典输出
f1(1, a=1,b=2,c=3)

#传字典
def f1(x, **kwargs):
    print(kwargs)  #{'a': 1, 'b': 4, 'c': 5, 'd': 6} #迭代输出
f1(1, **{'a':1, 'b':4, 'c':5, 'd':6})

#传字典
def f1(x, **kwargs):
    print(kwargs)  #{'a': 1, 'b': 4, 'c': 5, 'd': 6} #迭代输出
#f1(1, {'a':1, 'b':4, 'c':5, 'd':6})#报错
#传任何参数
def f1(x,*args,**kwargs):
    print(kwargs)  #{'y': 22323, 'a': 1, 'b': 4, 'c': 5, 'd': 6} #key=valu和字典参数,合并迭代输出字典
    print(args) #(2, 3, 5)
f1(1, 2,3,5,y=22323,**{'a':1, 'b':4, 'c':5, 'd':6})


#变量用域之全局global
name = 'lee'
def test():
    name = 'yongge'
    print(name) #yongge
    def test1():
        global name
        print(name) #lee
    test1()

test()


def test2():
    name='666'
    print(name) #666
    def test3():
        name = 'lee'
    test3()
    print(name) # 666
test2()

#变量用域之上一级
def test2():
    name='666'
    print(name) #666
    def test3():
        nonlocal name
        name = 'lee'
    test3()
    print(name) #lee
test2()
Logo

CSDN联合极客时间,共同打造面向开发者的精品内容学习社区,助力成长!

更多推荐