Home Backend Development Python Tutorial Introduction to decorators, generators and iterators in python

Introduction to decorators, generators and iterators in python

Jul 18, 2017 pm 03:25 PM
python

装饰器()

1、装饰器:本质是函数;

装饰器(装饰其他函数),就是为其他函数添加附加功能;

原则:1.不能修改被装饰函数的源代码;

   2.不能修改被装饰的函数的调用方式;

装饰器对被装饰的函数完全透明的,没有修改被装饰函数的代码和调用方式。

实现装饰器知识储备:

1.函数即“变量”;

2.高阶函数;

3.嵌套函数

高阶函数+嵌套函数=》装饰器

匿名函数(lambda表达式)

>>> calc = lambda x:x*3
>>> calc(2)
6

高阶函数:

  a.把一个函数名当做实参传递给另外一个函数;

>>> def bar():  print("in the bar.....")  
>>> def foo(func):
   print(func)>>> foo(bar)
<function bar at 0x7f8b3653cbf8>  
Copy after login


  b.返回值中包含函数名;

>>> import time
>>> def foo():  time.sleep(3)  print("in the foo.....")>>> def main(func):   print(func)   return func>>> t = main(foo)<function foo at 0x7fb7dc9e3378>>>> t()in the foo.....
Copy after login

在不修改源代码的情况下,统计程序运行的时间:

<span style="font-size: 16px">import time</span><br/><br/><span style="font-size: 16px">def timmer(func):</span><br/><span style="font-size: 16px">    def warpper(*args,**kwargs):   #warpper(*args,**kwargs)万能参数,可以指定参数,也可以不指定参数</span><br/><span style="font-size: 16px">        start_time = time.time()     #计算时间</span><br/><span style="font-size: 16px">        func()</span><br/><span style="font-size: 16px">        stop_time = time.time()</span><br/><span style="font-size: 16px">        print("the func run time is %s" %(stop_time-start_time)) #计算函数的运行时间</span><br/><span style="font-size: 16px">    return warpper</span><br/><br/><span style="font-size: 16px">@timmer    #等价于test1 = timmer(test1),因此函数的执行调用是在装饰器里面执行调用的</span><br/><span style="font-size: 16px">def test1():</span><br/><span style="font-size: 16px">    time.sleep(3)</span><br/><span style="font-size: 16px">    print("in the test1")</span><br/><br/><span style="font-size: 16px">test1()<br/>运行结果如下:<br/></span>
Copy after login

in the test1
the func run time is 3.001983404159546

装饰器带参数的情况:

<span style="font-size: 16px; font-family: 宋体">import time</span><br/><br/><span style="font-size: 16px; font-family: 宋体">def timmer(func):</span><br/><span style="font-size: 16px; font-family: 宋体">    def warpper(*args,**kwargs):</span><br/><span style="font-size: 16px; font-family: 宋体">        start_time = time.time()     #计算时间</span><br/><span style="font-size: 16px; font-family: 宋体">        func(*args,**kwargs)  #执行函数,装饰器参数情况</span><br/><span style="font-size: 16px; font-family: 宋体">        stop_time = time.time()</span><br/><span style="font-size: 16px; font-family: 宋体">        print("the func run time is %s" %(stop_time-start_time)) #计算函数的运行时间</span><br/><span style="font-size: 16px; font-family: 宋体">    return warpper    #返回内层函数名</span><br/><br/><span style="font-size: 16px; font-family: 宋体">@timmer</span><br/><span style="font-size: 16px; font-family: 宋体">def test1():</span><br/><span style="font-size: 16px; font-family: 宋体">    time.sleep(3)</span><br/><span style="font-size: 16px; font-family: 宋体">    print("in the test1")</span><br/><br/><span style="font-size: 16px; font-family: 宋体">@timmer    #test2 = timmer(test2)</span><br/><span style="font-size: 16px; font-family: 宋体">def test2(name):</span><br/><span style="font-size: 16px; font-family: 宋体">    print("in the test2 %s" %name)</span><br/><br/><span style="font-size: 16px; font-family: 宋体">test1()</span><br/><span style="font-size: 16px; font-family: 宋体">test2("alex")<br/>运行结果如下:<br/></span>
Copy after login

in the test1
the func run time is 3.0032410621643066
in the test2 alex
the func run time is 2.3603439331054688e-05

装饰器返回值情况:

import time
user,passwd = "alex","abc123"def auth(func):
    def wrapper(*args,**kwargs):
        username = input("Username:").strip()
        password = input("Password:").strip()if user == username and passwd == password:
            print("\033[32;1mUser has passed authentication.\033[0m")return func(*args,**kwargs)   #实际上执行调用的函数   
            # res = func(*args,**kwargs)
            # return res   #函数返回值的情况,因为装饰器调用的时候是在装饰器调用的函数,因此返回值也在这个函数中else:
            exit("\033[31;1mInvalid username or password.\033[0m")return wrapper


def index():
    print("welcome to index page...")

@auth
def home():
    #用户自己页面
    print("welcome to home page...")return "form home..."@auth
def bbs():
    print("welcome to bbs page")

index()
print(home())
bbs()
Copy after login

装饰器带参数的情况:

实现:1、本地验证;2、远程验证

import time
user,passwd = "alex","abc123"def auth(auth_type):&#39;&#39;&#39;函数的多层嵌套,先执行外层函数&#39;&#39;&#39;print("auth_type",auth_type)
    def out_wrapper(func):
        def wrapper(*args,**kwargs):
            print("wrapper func args:",*args,**kwargs)if auth_type == "local":
                username = input("Username:").strip()
                password = input("Password:").strip()if user == username and passwd == password:
                    print("\033[32;1mUser has passed authentication.\033[0m")
                    func(*args,**kwargs)   #实际上执行调用的函数
                    # res = func(*args,**kwargs)
                    # return res   #函数返回值的情况,因为装饰器调用的时候是在装饰器调用的函数,因此返回值也在这个函数中else:
                    exit("\033[31;1mInvalid username or password.\033[0m")
            elif auth_type == "ldap":
                print("搞毛线lbap,傻逼....")return wrapperreturn out_wrapper


def index():
    print("welcome to index page...")

@auth(auth_type="local")
def home():
    #用户自己页面
    print("welcome to home page...")return "form home..."@auth(auth_type="ldap")
def bbs():
    print("welcome to bbs page")

index()
home()
bbs()   #函数没有,因为没有调用函数,函数的调用在装饰器里面,是装饰器调用了函数
Copy after login

  迭代器和生成器

  生成器

  通过列表生成式,我们可以直接创建一个列表。但是,受到内存限制,列表容量肯定是有限的。而且,创建一个包含100万个元素的列表,不仅占用很大的存储空间,如果我们仅仅需要访问前面几个元素,那后面绝大多数元素占用的空间都白白浪费了。

  所以,如果列表元素可以按照某种算法推算出来,那我们是否可以在循环的过程中不断推算出后续的元素呢?这样就不必创建完整的list,从而节省大量的空间。在Python中,这种一边循环一边计算的机制,称为生成器:generator。

  >>> l1 = (i for i in range(10))
  >>> l1
   at 0x7f6a9fbcaeb8>
  >>> l1.__next__()
  0
  >>> l1.__next__()
  1
  生成器:只有在调用时才会生成相应的数据;

  只有通过__next__()方法进行执行,这种能够记录程序运行的状态,yield用来生成迭代器函数。(只能往后调用,不能向前或者往后推移,只记住当前状态,因此银行的系统用来记录的时候可以使用yield函数)。

 %=  %= consumer(= consumer( i  range(,)

运行如下:
A准备吃包子了......
B准备吃包子了......
包子1被A吃了......
包子1被B吃了......
包子2被A吃了......
包子2被B吃了......
包子3被A吃了......
包子3被B吃了......
包子4被A吃了......
包子4被B吃了......
包子5被A吃了......
包子5被B吃了......
包子6被A吃了......
包子6被B吃了......
包子7被A吃了......
包子7被B吃了......
包子8被A吃了......
包子8被B吃了......
包子9被A吃了......
包子9被B吃了.....
Copy after login

  迭代器

  我们已经知道,可以直接作用于for循环的数据类型有以下几种:

  一类是集合数据类型,如listtupledictsetstr等;

  一类是generator,包括生成器和带yield的generator function。

  这些可以直接作用于for循环的对象统称为可迭代对象:Iterable

  可以使用isinstance()判断一个对象是否是Iterable对象

>>> from collections import Iterable
>>> isinstance([], Iterable)
True
>>> isinstance({}, Iterable)
True
>>> isinstance(&#39;abc&#39;, Iterable)
True
>>> isinstance((x for x in range(10)), Iterable)
True
>>> isinstance(100, Iterable)
False
Copy after login

The above is the detailed content of Introduction to decorators, generators and iterators in python. For more information, please follow other related articles on the PHP Chinese website!

Statement of this Website
The content of this article is voluntarily contributed by netizens, and the copyright belongs to the original author. This site does not assume corresponding legal responsibility. If you find any content suspected of plagiarism or infringement, please contact admin@php.cn

Hot AI Tools

Undresser.AI Undress

Undresser.AI Undress

AI-powered app for creating realistic nude photos

AI Clothes Remover

AI Clothes Remover

Online AI tool for removing clothes from photos.

Undress AI Tool

Undress AI Tool

Undress images for free

Clothoff.io

Clothoff.io

AI clothes remover

Video Face Swap

Video Face Swap

Swap faces in any video effortlessly with our completely free AI face swap tool!

Hot Article

Roblox: Bubble Gum Simulator Infinity - How To Get And Use Royal Keys
3 weeks ago By 尊渡假赌尊渡假赌尊渡假赌
Nordhold: Fusion System, Explained
4 weeks ago By 尊渡假赌尊渡假赌尊渡假赌
Mandragora: Whispers Of The Witch Tree - How To Unlock The Grappling Hook
3 weeks ago By 尊渡假赌尊渡假赌尊渡假赌

Hot Tools

Notepad++7.3.1

Notepad++7.3.1

Easy-to-use and free code editor

SublimeText3 Chinese version

SublimeText3 Chinese version

Chinese version, very easy to use

Zend Studio 13.0.1

Zend Studio 13.0.1

Powerful PHP integrated development environment

Dreamweaver CS6

Dreamweaver CS6

Visual web development tools

SublimeText3 Mac version

SublimeText3 Mac version

God-level code editing software (SublimeText3)

Hot Topics

Java Tutorial
1670
14
PHP Tutorial
1274
29
C# Tutorial
1256
24
PHP and Python: Different Paradigms Explained PHP and Python: Different Paradigms Explained Apr 18, 2025 am 12:26 AM

PHP is mainly procedural programming, but also supports object-oriented programming (OOP); Python supports a variety of paradigms, including OOP, functional and procedural programming. PHP is suitable for web development, and Python is suitable for a variety of applications such as data analysis and machine learning.

Choosing Between PHP and Python: A Guide Choosing Between PHP and Python: A Guide Apr 18, 2025 am 12:24 AM

PHP is suitable for web development and rapid prototyping, and Python is suitable for data science and machine learning. 1.PHP is used for dynamic web development, with simple syntax and suitable for rapid development. 2. Python has concise syntax, is suitable for multiple fields, and has a strong library ecosystem.

How to run sublime code python How to run sublime code python Apr 16, 2025 am 08:48 AM

To run Python code in Sublime Text, you need to install the Python plug-in first, then create a .py file and write the code, and finally press Ctrl B to run the code, and the output will be displayed in the console.

PHP and Python: A Deep Dive into Their History PHP and Python: A Deep Dive into Their History Apr 18, 2025 am 12:25 AM

PHP originated in 1994 and was developed by RasmusLerdorf. It was originally used to track website visitors and gradually evolved into a server-side scripting language and was widely used in web development. Python was developed by Guidovan Rossum in the late 1980s and was first released in 1991. It emphasizes code readability and simplicity, and is suitable for scientific computing, data analysis and other fields.

Python vs. JavaScript: The Learning Curve and Ease of Use Python vs. JavaScript: The Learning Curve and Ease of Use Apr 16, 2025 am 12:12 AM

Python is more suitable for beginners, with a smooth learning curve and concise syntax; JavaScript is suitable for front-end development, with a steep learning curve and flexible syntax. 1. Python syntax is intuitive and suitable for data science and back-end development. 2. JavaScript is flexible and widely used in front-end and server-side programming.

Golang vs. Python: Performance and Scalability Golang vs. Python: Performance and Scalability Apr 19, 2025 am 12:18 AM

Golang is better than Python in terms of performance and scalability. 1) Golang's compilation-type characteristics and efficient concurrency model make it perform well in high concurrency scenarios. 2) Python, as an interpreted language, executes slowly, but can optimize performance through tools such as Cython.

Where to write code in vscode Where to write code in vscode Apr 15, 2025 pm 09:54 PM

Writing code in Visual Studio Code (VSCode) is simple and easy to use. Just install VSCode, create a project, select a language, create a file, write code, save and run it. The advantages of VSCode include cross-platform, free and open source, powerful features, rich extensions, and lightweight and fast.

How to run python with notepad How to run python with notepad Apr 16, 2025 pm 07:33 PM

Running Python code in Notepad requires the Python executable and NppExec plug-in to be installed. After installing Python and adding PATH to it, configure the command "python" and the parameter "{CURRENT_DIRECTORY}{FILE_NAME}" in the NppExec plug-in to run Python code in Notepad through the shortcut key "F6".

See all articles