一、命名空间与作用域

命名空间是名字和对象的映射,就像是字典,key是变量名,value是变量的值

1.命名空间的定义

name='egon'  #定义变量

def func():  #定义函数
pass class Foo:  #定义类
pass

2.命名空间的分类

  • 1.内置名称空间: 随着python解释器的启动而产生,包括异常类型、内建函数和特殊方法,可以代码中任意地方调用
print(sum)
print(max)
print(min) print(max([1,2,3])) import builtins
for i in dir(builtins): #打印所有的内置函数
print(i)

输出

<built-in function sum>
<built-in function max>
<built-in function min>
3
ArithmeticError
AssertionError
AttributeError
BaseException
BlockingIOError
BrokenPipeError
BufferError
BytesWarning
ChildProcessError
ConnectionAbortedError
ConnectionError
ConnectionRefusedError
ConnectionResetError
DeprecationWarning
EOFError
Ellipsis
EnvironmentError
Exception
False
FileExistsError
FileNotFoundError
FloatingPointError
FutureWarning
GeneratorExit
IOError
ImportError
ImportWarning
IndentationError
IndexError
InterruptedError
IsADirectoryError
KeyError
KeyboardInterrupt
LookupError
MemoryError
NameError
None
NotADirectoryError
NotImplemented
NotImplementedError
OSError
OverflowError
PendingDeprecationWarning
PermissionError
ProcessLookupError
RecursionError
ReferenceError
ResourceWarning
RuntimeError
RuntimeWarning
StopAsyncIteration
StopIteration
SyntaxError
SyntaxWarning
SystemError
SystemExit
TabError
TimeoutError
True
TypeError
UnboundLocalError
UnicodeDecodeError
UnicodeEncodeError
UnicodeError
UnicodeTranslateError
UnicodeWarning
UserWarning
ValueError
Warning
ZeroDivisionError
__build_class__
__debug__
__doc__
__import__
__loader__
__name__
__package__
__spec__
abs
all
any
ascii
bin
bool
bytearray
bytes
callable
chr
classmethod
compile
complex
copyright
credits
delattr
dict
dir
divmod
enumerate
eval
exec
exit
filter
float
format
frozenset
getattr
globals
hasattr
hash
help
hex
id
input
int
isinstance
issubclass
iter
len
license
list
locals
map
max
memoryview
min
next
object
oct
open
ord
pow
print
property
quit
range
repr
reversed
round
set
setattr
slice
sorted
staticmethod
str
sum
super
tuple
type
vars
zip
  • 2.全局名称空间:文件的执行会产生全局名称空间,指的是文件级别定义的名字都会放入该空间
x=1 #全局命名空间

def func():
money=2000 #非全局
x=2
print('func')
print(x)
print(func)
func()
  • 3.局部名称空间:调用函数时会产生局部名称空间,只在函数调用时临时绑定,调用结束解绑定
x=10000    #全局
def func():
x=1    #局部
def f1():
pass

3.作用域

命名空间的可见性就是作用域

  • 1. 全局作用域:内置名称空间,全局名称空间
  • 2. 局部作用域:局部名称空间

名字的查找顺序:局部名称空间---》全局名层空间---》内置名称空间

查看全局作用域内的名字:gloabls()

查看局部作用域内的名字:locals()

全局作用域的名字:全局有效,在任何位置都能被访问到,除非del删掉,否则会一直存活到文件执行完毕

局部作用域的名字:局部有效,只能在局部范围调用,只在函数调用时才有效,调用结束就失效

x=1000
def func(y):
x=2
print(locals())
print(globals()) func(1)

输出

{'y': 1, 'x': 2}
{'__loader__': <_frozen_importlib_external.SourceFileLoader object at 0x10c436c88>, '__package__': None, '__cached__': None, '__file__': '/Users/hexin/PycharmProjects/py3/day4/2.py', 'func': <function func at 0x10c3c9f28>, '__builtins__': <module 'builtins' (built-in)>, '__spec__': None, '__doc__': None, 'time': <module 'time' (built-in)>, '__name__': '__main__', 'x': 1000}

二、闭包函数

简单来说,一个闭包就是你调用了一个函数A,这个函数A返回了一个函数B给你。这个返回的函数B就叫做闭包。

闭包函数须满足以下条件:

1. 定义在内部函数;
2. 包含对外部作用域而非全局作用域的引用;

def f1():
x = 1
def f2():
print(x)
return f2 f=f1()
print(f) x=100
f()
print(x)

输出

<function f1.<locals>.f2 at 0x107714400>
1
100

闭包应用

from urllib.request import urlopen

def index(url):
def get():
return urlopen(url).read()
return get oldboy=index('http://crm.oldboyedu.com') print(oldboy().decode('utf-8'))

输出

<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="UTF-8">
<title>OldboyCRM</title>
<!--Bootstrap Stylesheet [ REQUIRED ]-->
<link href="/static/css/bootstrap.min.css" rel="stylesheet">
<link href="/static/css/custom.css" rel="stylesheet"> <!--Nifty Stylesheet [ REQUIRED ]-->
<link href="/static/css/nifty.min.css" rel="stylesheet"> <!--Font Awesome [ OPTIONAL ]-->
<link href="/static/plugins/font-awesome/css/font-awesome.min.css" rel="stylesheet"> <!--Bootstrap Validator [ OPTIONAL ]-->
<link href="/static/plugins/bootstrap-validator/bootstrapValidator.min.css" rel="stylesheet"> <!--Demo [ DEMONSTRATION ]-->
<link href="/static/css/demo/nifty-demo.min.css" rel="stylesheet"> <!--Bootstrap Datepicker [ OPTIONAL ]-->
<link href="/static/plugins/bootstrap-datepicker/bootstrap-datepicker.css" rel="stylesheet"> </head>
<body> <div id="container" class="effect mainnav-lg">
<div id="page-title">
<h1 class="page-header text-overflow">老男孩IT教育 | 只培养技术精英</h1> </div> <div id="page-content"> <div class="row"> <div class="col-lg-12"> <div class="panel">
<div class="panel-heading">
<h3 class="panel-title">学员平台</h3>
</div>
<div class="panel-body" style=""> <h4><a class="btn-link" href="/grade/single/">成绩查询</a></h4>
<h4><a class="btn-link" href="/scholarship/">奖学金政策</a></h4>
<h4><a class="btn-link" href="/training_contract/">培训协议查询</a></h4>
<h4><a class="btn-link" href="/compliant/">投诉建议</a></h4>
<h4><a class="btn-link" href="/stu_faq/">学员常见问题汇总</a></h4>
<h4><a class="btn-link" href="/stu/">学员登录</a></h4> </div> <!--end panel-body-->
</div> <!--end panel-->
</div> <!--end col-lg-12-->
</div><!--end row--> </div><!--end page-content--> </div> <!--jQuery [ REQUIRED ]-->
<script src="/static/js/jquery-2.1.1.min.js"></script> <!--BootstrapJS [ RECOMMENDED ]-->
<script src="/static/js/bootstrap.min.js"></script> <!--Nifty Admin [ RECOMMENDED ]-->
<script src="/static/js/nifty.min.js"></script> <!--jquery-cookie-->
<script src="/static/js/jquery.cookie.js"></script> <script src="/static/js/ajax_comm.js"></script> <!--Bootstrap Wizard [ OPTIONAL ]-->
<script src="/static/plugins/bootstrap-wizard/jquery.bootstrap.wizard.min.js"></script> <!--Bootstrap Validator [ OPTIONAL ]-->
<script src="/static/plugins/bootstrap-validator/bootstrapValidator.min.js"></script> <!--Demo script [ DEMONSTRATION ]-->
<script src="/static/js/demo/nifty-demo.min.js"></script> <!--Form Wizard [ SAMPLE ]-->
<script src="/static/js/demo/form-wizard.js"></script> <!--Bootstrap Datepicker [ OPTIONAL ]-->
<script src="/static/plugins/bootstrap-datepicker/bootstrap-datepicker.js"></script> </body>
</html>

三、装饰器

1.定义

装饰器:修饰别人的工具,修饰添加功能,工具指的是函数

装饰器本身可以是任何可调用对象,被装饰的对象也可以是任意可调用对象

2.为什么要用装饰器?

开放封闭原则:对修改是封闭的,对扩展是开放的
装饰器就是为了在不修改被装饰对象的源代码以及调用方式的前提下,为其添加新功能

3.装饰器的实现

装饰器的功能是将被装饰的函数当作参数传递给与装饰器对应的函数(名称相同的函数),并返回包装后的被装饰的函数”

直接看示意图,其中 a 为与装饰器 @a 对应的函数, b 为装饰器修饰的函数,装饰器@a的作用是:

简而言之:@a 就是将 b 传递给 a(),并返回新的 b = a(b)

例如

def a(name):      #与装饰器对应的函数
return name() @a            #装饰器 b = a(b)
def b():        #被装饰函数
print('hexin')

输出

hexin

解析过程是这样子的:
1.python 解释器发现@a,就去调用与其对应的函数( a 函数)
2.a 函数调用前要指定一个参数,传入的就是@a下面修饰的函数,也就是 b()
3.a() 函数执行,调用 b(),b() 打印“hexin”

5.装饰器的应用

import time

def timmer(func):
def wrapper():
start_time=time.time()
func()       #index()
stop_time=time.time()
print('run time is %s' %(stop_time-start_time))
return wrapper @timmer       #index=timmer(index)
def index():
time.sleep(1)
print('welcome to index') index()

输出

welcome to index
run time is 1.005241870880127

例子

login_user={'user':None,'status':False}
def auth(func):
def wrapper(*args,**kwargs):
if login_user['user'] and login_user['status']:
res=func(*args,**kwargs)
return res
else:
name=input('请输入用户名: ')
password=input('请输入密码: ')
if name == 'hexin' and password == '':
login_user['user']='hexin'
login_user['status']=True
print('\033[45mlogin successful\033[0m')
res=func(*args,**kwargs)
return res
else:
print('\033[45mlogin err\033[0m')
return wrapper @auth #index=auth(index)
def index():
print('welcome to index page') @auth #home=auth(home)
def home(name):
print('%s welcome to home page' %name) index()
home('hexin')

输出

请输入用户名: heixn
请输入密码: 123
login err
请输入用户名: hexin
请输入密码: 123
login successful
hexin welcome to home page

补充:

装饰器的基本框架:

def timer(func):
def wrapper():
func()
return wrapper

带参数

def timer(func):
def wrapper(*args,**kwargs):
func(*args,**kwargs)
return wrapper

【Python3的命名空间与作用域,闭包函数,装饰器】的更多相关文章

  1. 【Python 函数对象 命名空间与作用域 闭包函数 装饰器 迭代器 内置函数】

    一.函数对象 函数(Function)作为程序语言中不可或缺的一部分,但函数作为第一类对象(First-Class Object)却是 Python 函数的一大特性. 那到底什么是第一类对象(Firs ...

  2. Python--函数对象@命名空间与作用域@包函数@装饰器@迭代器@内置函数

    一.函数对象 函数(Function)作为程序语言中不可或缺的一部分,但函数作为第一类对象(First-Class Object)却是 Python 函数的一大特性. 那到底什么是第一类对象(Firs ...

  3. Python记录9:函数4:名称空间作用域+闭包函数+装饰器

    ''' 一: 名称空间namespaces     名称空间就是存放名字与值绑定关系的内存空间 二: 名称空间分为三种     内置名称空间:         1. 特点: 存放是python解释器自 ...

  4. Python作用域-->闭包函数-->装饰器

    1.作用域: 在python中,作用域分为两种:全局作用域和局部作用域. 全局作用域是定义在文件级别的变量,函数名.而局部作用域,则是定义函数内部. 关于作用域,我要理解两点:a.在全局不能访问到局部 ...

  5. python3 闭包函数 装饰器

    闭包函数 1.闭:定义在函数内部的函数 2.包:内部函数引用了外部函数作用域的名字 在函数编程中经常用到闭包.闭包是什么,它是怎么产生的及用来解决什么问题呢.给出字面的定义先:闭包是由函数及其相关的引 ...

  6. python 内嵌函数, 闭包, 函数装饰器

    一.  函数内嵌 闭包 在python中,函数可以作为返回值, 可以给变量赋值. 在python中, 内置函数必须被显示的调用, 否则不会执行. #!/usr/bin/env python #-*- ...

  7. Day 12 闭包函数,装饰器

    闭包函数 回顾: 1.函数对象:可以将定义在函数内的函数返回到全局使用.从而打破了函数层级限制 2.名称空间与作用域:作用域关系在函数定义阶段时就已经固定死了,与调用位置无关,即在任意位置调用函数都需 ...

  8. 【0812 | Day 13】闭包函数/装饰器/迭代器

    目录 闭包函数 无参装饰器 有参装饰器 迭代器 闭包函数 一.什么是闭包? 闭包指的是:函数内部函数对外部作用域而非全局作用域的引用. def outter(): x = 1 def inner(): ...

  9. python闭包函数&装饰器

    一.函数引用 函数可以被引用 函数可以被赋值给一个变量 def hogwarts(): print("hogwarts") # hogwarts() # 函数调用 print(ho ...

  10. Python3.5-20190516-廖老师-自我笔记-匿名函数-装饰器

    当函数很简单的时候采用匿名函数很方便.

随机推荐

  1. 五、利用EnterpriseFrameWork快速开发基于WebServices的接口

    回<[开源]EnterpriseFrameWork框架系列文章索引> EnterpriseFrameWork框架实例源代码下载: 实例下载 前面几章已完成EnterpriseFrameWo ...

  2. JVM自动内存管理机制--读这篇就GO了

    之前看过JVM的相关知识,当时没有留下任何学习成果物,有些遗憾.这次重新复习了下,并通过博客来做下笔记(只能记录一部分,因为写博客真的很花时间),也给其他同行一些知识分享. Java自动内存管理机制包 ...

  3. python-将爬取到的m3u8合并为mp4

    当你看到这个博客的时候恭喜你,你以后不用开vip就可以观看和下载vip视频了 最简单的观看vip视频步骤:进入全民解析网将vip视频地址进行解析 以下代码是通过python将vip视频进行下载为mp4 ...

  4. 001----Mysql隔离级别

    一:事务隔离级别 mysql数据库的隔离界别如下: 1, READ UNCOMMITTED(未提交读) 事务中的修改,即使没有提交,对其它事务也是可见的.  这样会造成脏读(Dirty Read)的问 ...

  5. java之接口开发-初级篇-socket通信

    socket通信实现util包类实现 public class SocketThread extends Thread { public void run() { while (true) { // ...

  6. Oracle 11g用exp无法导出空表的处理方法

    Oracle 11G在用EXPORT导出时,空表不能导出. 11G中有个新特性,当表无数据时,不分配segment,以节省空间 解决方法: 1.insert一行,再rollback就产生segment ...

  7. 到底什么是BFC、IFC、GFC和FFC,次奥?

    软件开发的一般被称为民工,搞前端的,有人形容为是掏粪工,说白了连民工级别高都没有.说直接点就是个制作界面的,注意,连设计界面的都算不上,一般前端都是拿着设计稿去照这样子开发的. 说这些无非是觉得前端前 ...

  8. 欢迎来怼--第二十三次Scrum会议

    一.小组信息 队名:欢迎来怼 小组成员 队长:田继平 成员:李圆圆,葛美义,王伟东,姜珊,邵朔,阚博文 小组照片 二.开会信息 时间:2017/11/11 17:20~17:55,总计35min. 地 ...

  9. 冲刺ing-6

    第六次Scrum冲刺 队员完成的任务 队员 完成任务 吴伟华 Leangoo的看板截图,燃尽图 蔺皓雯 编写博客,界面设计 蔡晨旸 界面设计 曾茜 测试 鲁婧楠 学习后端设计 杨池宇 界面设计 成员遇 ...

  10. 用python脚本计算某一个文件的行数

    python可以统计文件的行数,你相信吗?不管你信不信反正我信了.下面我们来看一下python怎样统计文件的行数,代码很简单,我也做了注释,很简单的实现... 1 2 3 4 5 6 7 8 9 10 ...