Python trivia point (3)--Decorator

Source: Internet
Author: User
Tags iterable ldap wrapper

(1) The adorner contains parameters, the decoration function does not contain (including) parameters

The instance code is as follows:

Import Time#Adorner FunctionsdefWrapper (func):defDone (*args,**Kwargs): Start_time=time.time () func (*args,**Kwargs) Stop_time=time.time ()Print('The func run time is%s'% (Stop_time-start_time)) return Done#be decorated function 1@wrapperdeftest1 (): Time.sleep (1)    Print("In the test1")#be decorated function 2@wrapperdefTest2 (name):#1.test2===>wrapper (test2) 2.test2 (name) ==dome (name )Time.sleep (2)    Print("In the test2,the Arg is%s"%name)#calledtest1 () test2 ("Hello World")

(2) the adorner contains parameters and is decorated with (not included) parameters

Importtimeuser,passwd='Admin','Admin'defAuth (auth_type):Print("auth func:", Auth_type)defOuter_wrapper (func):defWrapper (*args, * *Kwargs):Print("wrapper func args:", *args, * *Kwargs)ifAuth_type = ="Local": Username= Input ("Username:"). Strip () password= Input ("Password:"). Strip ()ifuser = = Username andpasswd = =Password:Print("\033[32;1muser has passed authentication\033[0m") Res= Func (*args, **kwargs)#From home                    Print("---after authenticaion")                    returnResElse: Exit ("\033[31;1minvalid username or password\033[0m")            elifAuth_type = ="LDAP":                Print("LDAP link")        returnwrapperreturnOuter_wrapper@auth (Auth_type="Local")#home = wrapper ()defHome ():Print("Welcome to Home Page")    return "From home"@auth (Auth_type="LDAP")defBBS ():Print("Welcome to BBS page"Print(Home ())#wrapper ()BBS ()

Summarize:

(1) The adorner is essentially a function inline, returning the function address.

(2) adorner with parameters and without parameters more than the adorner with parameters a layer of function definitions are used to receive parameters passed in the adorner, the rest is basically the same.

(3) First verify the parameters in the adorner, and verify the parameters of the normal function.

Small knowledge:

list production Type: [I for I in range (5)]---->[0,1,2,3,4,5]

Generators and iterators:

The first method is generated by means of parentheses

generator: ()---(I for I in range (5)) ==>generator

This side loop computes the mechanism, called the generator:generator.

The generator generates the corresponding data only when it is called, only the current position is recorded.

there is only one __next__ () method

The second way is generated by yield

use yield in a function to turn a function into a generator

Iterators:

The data type that directly acts on the For loop:

A class is a collection of data types, such as list,tuple,dict,set, Str and so on;

A class is generator, including generators and generator functionwith yield .

An object that acts directly on A for loop is called an iterative object:iterable.

You can use isinstance () to determine whether an object is a iterable Object

From collections Import iterable

Isinstance ([], iterable) =========true

An object that can be called by the next () function and continually returns the next value is called an iterator:Iterator.

You can use isinstance () to determine whether an object is a Iterator object:

>>> from collections Import Iterator

>>> Isinstance ((x for X in range), Iterator)

======>true

generators are iterator list dict str Although it is iterable iterator

the list,dict,str and other iterable into Iterator You can use the iter () function:

Example:iter ([]) <==== iterator

Python iterator object represents a data flow, Iterator next () function calls and returns the next data continuously until no data is thrown stopiteration error. You can think of this data stream as an ordered sequence, but we can't know the length of the sequence in advance, only through the next () The function enables the next data to be computed on demand, so iterator

Iterator can even represent an infinitely large stream of data, such as the whole natural number. Using list is never possible to store all natural numbers.

Summary:

all objects that can be used for a for loop are iterable types;

all objects that can be used for the next () function are Iterator types, which represent a sequence of lazy computations;

collection data types such as list,dict,str , etc. are iterable but not Iterator, however, a Iterator object can be obtained through the iter () function .

Python trivia point (3)--Decorator

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.