Python full stack-tenth study notes

Python tenth study notes

Parameter angle

  • Universal parameters.

  • * And ** magic usage.

  • # 万能参数
    # 下面的例子相当于写死了程序,未来添加功能不方便
    def eat(a,b,c,d):
    	print('我请你吃:%s,%s,%s,%s' %(a,b,c,d))
        
    eat('蒸羊羔', '蒸熊掌', '蒸鹿邑','烧花鸭')
    
    # 未来改的时候只能在源码上改
    def eat(a,b,c,d,e,f):
    	print('我请你吃:%s,%s,%s,%s,%s,%s' %(a,b,c,d,e,f))
        
    eat('蒸羊羔', '蒸熊掌', '蒸鹿邑','烧花鸭','烧雏鸡','烧子鹅')
    
    # 急需要一种形参,可以接受所有的实参。  万能参数
    # 万能参数: *args, 约定俗称:args
    # 函数定义时,*代表聚合。他将所有的位置参数聚合成一个元组,赋值给了args。
    def eat(*args):
    	print(args)
    	print('我请你吃:%s,%s,%s,%s,%s,%s' % args)
        
    eat('蒸羊羔', '蒸熊掌', '蒸鹿邑','烧花鸭','烧雏鸡','烧子鹅')
    
    # 写一个函数:计算你传入函数的所有的数字的和。
    def func(*args):
    	count = 0
    	for i in args:
    		count += i
    	return count
    print(func(1,2,3,4,5,6,7))
    
    # **kwargs
    # 函数的定义时:** 将所有的关键字参数聚合到一个字典中,将这个字典赋值给了kwargs.
    def func(**kwargs):
    	print(kwargs)
    func(name='jarvis',age=18,sex='boy')
    
    # 万能参数:*args, **kwargs,
    def func(*args,**kwargs):
    	print(args)
    	print(kwargs)
    func()
    print()
    
    # * **在函数的调用时,*代表打散。
    def func(*args,**kwargs):
        print(args) # (1,2,3,22,33)
        print(kwargs)
        
    func(*[1,2,3],*[22,33])  		# func(1,2,3,22,33) 将两个列表打散
    
    func(**{'name': 'jarvis'},**{'age': 18})  # func(name='jarvis',age='18') 将两个字典打散
    
  • Keyword parameters only (understand)

  • Final order of formal parameters

    # 形参角度的参数的顺序
    # *args 的位置?
    # 错误的位置
    def func(*args,a,b,sex= '男'):
    	print(a,b)
    func(1,2,3,4)
    
    # args得到实参的前提,sex必须被覆盖了。
    # 方法一
    def func(a,b,sex= '男',*args,):
    	print(a,b)
    	print(sex)
    	print(args)
    func(1,2,3,4,5,6,7,)
    
    #方法二
    def func(a,b,*args,sex= '男'):
    	print(a,b)
    	print(sex)
    	print(args)
    func(1,2,3,4,5,6,7,sex='女')
    
    # **kwargs 位置?
    def func(a,b,*args,sex= '男',**kwargs,):
        print(a,b)
        print(sex)
        print(args)
        print(kwargs)
    func(1,2,3,4,5,6,7,sex='女',name='jarvis',age=80)
    
    # 形参角度第四个参数:仅限关键字参数 (了解)
    # 仅限关键字参数必须在*args和**kwargs中间,并且只能通过关键值参数进行传值
    def func(a,b,*args,sex= '男',c,**kwargs,):
    	print(a,b)
        print(sex)
        print(args)
        print(c)
        print(kwargs)
    func(1,2,3,4,5,6,7,sex='女',name='Alex',age=80,c='666')
    
    # 形参角度最终的顺序:位置参数,*args,默认参数,仅限关键字参数,**kwargs
    

Namespace

  • After the Python interpreter starts to execute 行, it will open up a space in memory. Whenever a variable 量 is encountered, the relationship between the variable 量 name and value is recorded, but when the function definition is encountered, the interpreter is just Reading the function name into memory indicates that this function exists, and as for the internal variables and logic of the function, the interpreter does not care. That is to say, the function is only loaded at the beginning, and nothing more, only when the function is called and When accessing, the interpreter will enter into the internal space of the variable according to the variable declared inside the function. With the completion of the function execution, the space occupied by the internal change of these functions will be cleared as the function execution finishes.

  • Let's first recall how the function is encountered when the Python code is running. After the Python interpreter starts execution, it opens up a space in memory. Whenever a variable is encountered, the variable name and value are The corresponding relationship is recorded, but when the function definition is encountered, the interpreter just reads the function name into memory symbolically, indicating that it knows that the function exists. As for the variables and logic inside the function, the interpreter does not care at all.

  • When the function call is executed, the Python interpreter will open up a memory to store the content of this function. At this time, we will pay attention to what variables are in the function, and the variables in the function are stored in the newly opened memory. The variables in the function can only be used inside the function, and as the function is executed, all the contents of this memory will be cleared.

  • We have given a name to this space where 'the relationship between name and value' is stored ----- namespace.

  • At the beginning of the code, the space created to store "the relationship between variable names and values" is called the global namespace;

  • The temporary space created during the operation of the function is called a local namespace, also called a temporary namespace.

  • # 名称空间;命名空间。
    a = 1      		#开辟全局名称空间
    b = 2			#放入全局名称空间
    def func():		#放入全局名称空间
    	f = 5		#放入局部名称空间
    	print(f)	#执行完局部名称空间消失
    c = 3			#放入全局名称空间
    func()			#开辟局部名称空间
    
    # 内置名称空间:python源码给你提供的一些内置的函数,print input
    # python分为三个空间:
    # 内置名称空间(builtins.py)
    # 全局名称空间(当前py文件)
    # 局部名称空间(函数,函数执行时才开辟)
    
    # 加载顺序:
    # 内置名称空间 ---> 全局名称空间  ----> 局部名称空间(函数执行时)
    def func():		#放入全局名称空间
    	pass
    func()			#开辟局部名称空间
    a = 5			#放入全局名称空间
    
  • # 取值顺序(就近原则) 单向不可逆
    # (从局部找时)局部名称空间  ---> 全局名称空间  --->  内置名称名称空间
    # LEGB原则
    # L:local
    # E:enclosed
    # G:global
    # B:built-in
    name = '1234'
    def func():
        name = 'jarvis'
        print(name)
        
    func()
    
  • Scope

  • # python两个作用域:
    # 全局作用域:内置名称空间 全局名称空间
    # 局部作用域:局部名称空间
    
    # 局部作用域可以引用(无法改变)全局作用域的变量
    date = '周五'
    def func():
    	a = 666
    	print(date)
    	print(a)
    func()
    
    # 局部作用域不能改变全局变量。
    count = 1
    def func():
    	count += 2
    	print(count)
    func()  
    # local variable 'count' referenced before assignment
    # 局部作用域不能改变全局作用域的变量,当python解释器读取到局部作用域时,发现了你对一个变量进行修改的操作,解释器会认为你在局部已经定义过这个局部变量了,他就从局部找这个局部变量,报错了。
    
    # 使用可以,不能改变
    # local variable 'count' referenced before assignment
    def func():
        count = 1
        def inner():
            count += 1
            print(count)
        inner()
    func()
    
  • Nesting of functions (higher order functions)

  • # 练习题
    # 例1(分别打印什么)
    def func1():
        print('in func1')
        print(3)
    
    def func2():
        print('in func2')
        print(4)
    
    func1()
    print(1)
    func2()
    print(2)
    # in func1 3 1 in func2 4 2
    
    
    # 例2(分别打印什么)
    def func1():
        print('in func1')
        print(3)
    
    def func2():
        print('in func2')
        func1()
        print(4)
    
    print(1)
    func2()
    print(2)
    
    
    # 例3(分别打印什么)
    def fun2():
        print(2)
    
        def fun3():
            print(6)
    
        print(4)
        fun3()
        print(8)
    
    print(3)
    fun2()
    print(5)
    
  • Built-in function globals () locals ()

  • """
    本文件:研究内置函数:globals locals
    """
    a = 1
    b = 2
    def func():
        name = 'jarvis'
        age = 18
        print(globals())  	# 返回的是字典:字典里面的键值对:全局作用域的所有内容。
        print(locals())  	# 返回的是字典:字典里面的键值对:当前作用域的所有的内容。
    print(globals())  		# 返回的是字典:字典里面的键值对:全局作用域的所有内容。
    print(locals())  		# 返回的是字典:字典里面的键值对:当前作用域的所有的内容。
    func()
    

Guess you like

Origin www.cnblogs.com/rgz-blog/p/12716977.html