"Safe Cow Learning notes" python instance

Source: Internet
Author: User
Tags closure

Closed Package

What is a closure package ?
Simply put , closures are based on different configuration information to get different results and then take a look at the professional explanation : closures (Closure) are short for lexical closures (Lexical Closure) and are functions that reference free variables. This quoted free variable will exist with this function, even if it has left the environment in which it was created. So, there is another argument that closures are entities that are composed of functions and their associated reference environment.

python Example to see the concept is always confusing. , See a few python A small example would be.

Example 1
def make_adder (addend):

def adder (augend):
return augend + addend

Return adder

p = make_adder (23°c) q = Make_adder (44)

Print p (+) print Q (100)

Run Results : 123
144

Analyze:We found, Make_adderis a function,include a parameterAddend,The special place is that this function also defines a new function.,one of the variables in this new function is just the outerMake_adderThe Parameters.Which means,the external pass.Addendparameters have been andAdderthe function is bound together.,A new function has been formed,we can putAddendas a configuration information for the new function,configuration information is different,functions don't have the same function.,which is the function that can be customized..look at the results of the operation,We found,AlthoughPand theQ's AllMake_addergenerated by,but because the configuration parameters are different,a different result is obtained after the function of the same parameter is executed later.This is the closure ..

Example 2
def hellocounter (name):

COUNT=[0]
Def counter ():

Count[0]+=1

print ' Hello ', ', Name, ', ', str (count[0]) + ' access! ' Return counter

Hello = Hellocounter (' ma6174 ') Hello ()
Hello ()
Hello ()

Execution results
Hello, YSISL, 1 access! Hello, YSISL, 2 access! Hello, YSISL, 3 access!

AnalyzeThis program is more interesting,we can think of this program as a function of counting the number of function calls.. count[0]can be seen as a counter,didn't do it once .Hellofunction, Count[0]The value is added1. Maybe you'll have a question .:Why don't you just writeCountinstead, use a list?this isPython2of aBugs,If you don't use a list.,will report such a mistake:
Unboundlocalerror:local variable ' count ' referenced before assignment.What do you mean??meansConutThis variable is directly referenced without your definition.,I don't know what this is.,the program just collapsed..so,againPython3inside,a keyword was introduced: nonlocal,What is this key word for??is to tellpythonprogram,my one.Countvariables are externally defined.,you go outside and find out..thenpythonjust go to the outer function to find,and then we found it.count=0this definition and assignment,The program will be executed properly..
Python3Code
def hellocounter (name):

Count=0
Def counter ():

nonlocal count
Count+=1
print ' Hello ', ', Name, ', ', str (count[0]) + ' access! '

Return counter

Hello = Hellocounter (' ma6174 ') Hello ()
Hello ()
Hello ()

This note is for safe Cattle class student notes, want to see this course or information security of dry goods can go to safe cattle classes

security+ Certification Why is the Internet + era of the most popular certification?


Manifesto first introduce you to security+


security+ certification is a neutral third-party certification, the issuing agency for the United States Computer Industry Association CompTIA, and CISSP, ITIL and other common inclusion of the international IT Industry 10 Popular certification, and CISSP emphasis on information security management, compared to security+ Authentication is more emphasis on information security technology and operations.

This certification demonstrates your ability to network security, compliance and operational security, threats and vulnerabilities, application, data and Host security, access control and identity management, and encryption technology. Because of its difficult examination difficulty, the gold content is high, has been widely adopted by global enterprises and security professionals.

Why is security+ certification so hot?

Reason three: grounding gas, international stylish, easy to test, moderate cost!

As the most influential global leader in the global ICT sector, CompTIA is professional, fair and impartial in the field of information security talent certification. Security+ certification is highly operational and closely related to the daily work of frontline engineers. Suitable for banks, securities, insurance, internet companies and other IT-related personnel learning. As an international certification in 147 countries around the world are widely recognized.

Under the current tide of information security, talent is the key to the development of information security. and the current domestic information security personnel is very scarce, I believe security+ certification will become the most popular information security certification.

This article is from the "11662938" blog, please be sure to keep this source http://11672938.blog.51cto.com/11662938/1974440

"Safe Cow Learning notes" python instance

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.