博客
关于我
强烈建议你试试无所不能的chatGPT,快点击我
Python 学习笔记(三)Function
阅读量:5952 次
发布时间:2019-06-19

本文共 2989 字,大约阅读时间需要 9 分钟。

python引用变量的顺序: 当前作用域局部变量->外层作用域变量->当前模块中的全局变量->python内置变量

1. Scope:

• If a variable is assigned inside a def, it is local to that function.

• If a variable is assigned in an enclosing def, it is nonlocal to nested functions.
• If a variable is assigned outside all defs, it is global to the entire file.

• global makes scope lookup begin in the enclosing module’s scope and allows

names there to be assigned. Scope lookup continues on to the built-in scope if the
name does not exist in the module, but assignments to global names always create
or change them in the module’s scope.
• nonlocal restricts scope lookup to just enclosing defs, requires that the names already
exist there, and allows them to be assigned. Scope lookup does not continue
on to the global or built-in scopes.

global关键字用来在函数或其他局部作用域中使用全局变量。但是如果不修改全局变量也可以不使用global关键字。

>>> gcount = 0>>> def func():...     print(gcount)...     >>> def func_count():...     global gcount...     gcount +=1...     return gcount... >>> def func_count_test():...     print(func_count())...     print(func_count())12

nonlocal关键字用来在函数或其他作用域中使用外层(非全局)变量。

>>> def tester(start):... state = start # Each call gets its own state... def nested(label):... nonlocal state # Remembers state in enclosing scope... print(label, state)... state += 1 # Allowed to change it if nonlocal... return nested...>>> F = tester(0)>>> F('spam') # Increments state on each callspam 0>>> F('ham')ham 1>>> F('eggs')eggs 2
>>> spam = 99>>> def tester():... def nested():... nonlocal spam # Must be in a def, not the module!... print('Current=', spam)... spam += 1... return nested...SyntaxError: no binding for nonlocal 'spam' found

2. Arguments

  • Immutable arguments are effectively passed “by value.” (int,string,tuple) (复制)
  • Mutable arguments are effectively passed “by pointer.” (list, dictionary) (引用)
>>> def changer(a, b): # Arguments assigned references to objects... a = 2 # Changes local name's value only... b[0] = 'spam' # Changes shared object in-place...>>> X = 1>>> L = [1, 2] # Caller>>> changer(X, L) # Pass immutable and mutable objects>>> X, L # X is unchanged, L is different!(1, ['spam', 2])
def changer(a, b):b = b[:] # Copy input list so we don't impact caller
>>> def multiple(x, y):...     x=2...     y=[3,4]...     return x,y... >>> X =1>>> L=[1,2]>>> X,L = multiple(X,L)>>> X2>>> L[3, 4]>>> X,L(2, [3, 4])

参数传递:*代表的是tuple,**代表map

>>> def echo(*args, **kwargs): print(args, kwargs)...>>> echo(1, 2, a=3, b=4)(1, 2) {
'a': 3, 'b': 4}

intersect and union:

def intersect(*args):  res = []  for x in args[0]: # Scan first sequence    for other in args[1:]: # For all other args      if x not in other: break # Item in each one?    else: # No: break out of loop      res.append(x) # Yes: add items to end  return resdef union(*args):  res = []  for seq in args: # For all args    for x in seq: # For all nodes      if not x in res:        res.append(x) # Add new items to result  return res

 

转载于:https://www.cnblogs.com/zyf7630/p/3093279.html

你可能感兴趣的文章
推荐5大开源工具,用于开发Kubernetes项目
查看>>
制定2015年的移动开发策略
查看>>
JPA 2.2改进了易用性
查看>>
从蚂蚁金服实践入手,带你深入了解 Service Mesh
查看>>
24周年,“常青树”Delphi发布新版本10.3.1
查看>>
7. 从数据库获取数据- 从零开始学Laravel
查看>>
阿里百川码力APP监控 来了!
查看>>
使用dotenv管理环境变量
查看>>
温故js系列(11)-BOM
查看>>
Vuex学习
查看>>
bootstrap - navbar
查看>>
切图崽的自我修养-[ES6] 编程风格规范
查看>>
[React Native Android 安利系列]样式与布局的书写
查看>>
利用dxflib读写cad文件
查看>>
《DOM编程艺术》中CSS—DOM的总结(一)
查看>>
服务器迁移小记
查看>>
FastDFS存储服务器部署
查看>>
Android — 创建和修改 Fragment 的方法及相关注意事项
查看>>
(cons '(贰 . 输入/输出) 《为自己写本-Guile-书》)
查看>>
this全面解析(二)
查看>>