Python decorator
An adorner is essentially a function that handles other functions, allowing other functions to be used without the need to modify the code.
Added additional functionality, the return value of the adorner is also a function object. It is often used in scenarios where there is a need for facets, such as: plug
Application scenarios such as logging, performance testing, transaction processing, caching, and permission validation. Decorators are a great design for solving this kind of problem,
With adorners, we can pull out a lot of similar code that is not related to the function itself and continue to reuse it. Generally speaking, decoration
The function of a device is to add additional functionality to an already existing object.
A simple decorator
Cases:
def decro (func):d EF fn ():
Print "Before Lanuching"
return func () return FN
In the above we define a function of the Decro () function, which first returns a function called FN (), and in the interior of Decro has defined the FN () function, the FN () function first printed "Before Lanuching", and then called the function func ()
>>> def f ():
... print "HelloWorld"
...
>>> def Decro (func):
...
...
...
...
...
>>> F=decro (f) >>> F ()
DEF fn ():
Print "Before Lanuching" return func ()
RETURN fn
Before Lanuchinghelloworld
Red Envelope function
#!/usr/bin/env python
Import Random
def readpacket (People,money):
Result=[]
Remain=people
For I in range (people):
Remain=remain-1
If remain>0:
M=random.randint (1,money/remain)
Money=money-m
Result.append (M)
Else
Result.append (Money)
return result
Closed Package
What is a closure package?
Simply put, closures are based on different configuration information to get different results to see the professional explanation: Closure (Closure) is a lexical closure (Lexical Closure) abbreviation, is a reference to the function of the free variable. 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 instance look at the concept is always confusing the mind, see a few Python small examples will 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)
Operation Result: 123
144
Analysis: we found that Make_adder is a function, including a parameter addend, the more special place is the function inside the definition of a new function, the new function inside a variable is exactly the external make_adder parameters. That is to say, The externally passed Addend parameter has been bound together with the Adder function, forming a new function, we can think of addend as a new function of the configuration information, configuration information, functions of the function is different, that is, the function can be customized. Look at the results of the operation, we found that Although P and Q are make_adder generated, different results are obtained because of different configuration parameters, followed by functions that perform the same parameters. 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!
Analyze This program is interesting, we can think of this program as a function to count the number of function calls. Count[0] can be seen as a counter, without executing the Hello function, the value of count[0] is added 1. Maybe you have a question: why not just write count and use a list? This is a bug in Python2, and if you do not use a list, you will be quoted an error:
Unboundlocalerror:local variable ' count ' referenced before assignment. What do you mean, conut this variable you don't have a definition for, and I don't know what this is, The program collapsed. So, again python3 inside, introduced a keyword: nonlocal, this keyword is what? Just tell the Python program that my count variable is externally defined, you go outside and look for it. Then python goes to the outer function to find it, Then we find the definition and assignment of count=0, and the program will execute normally.
Python3 Code
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 one: In all information security certification, the emphasis on information security technology certification is blank, security+ certification can make up for the gap in the field of information security technology.
currently recognized in the industry of information security certification mainly Cisp and CISSP, but whether cisp or CISSP are emphasis on information security management, technical knowledge is broad and simple, the exam is around. And CISSP require a certificate of information security work experience for more than 5 years, Cisp also require a college education 4 years of working experience, these requirements will undoubtedly be able and motivated young people of the road blocked. In the real world, whether it is looking for a job or a raise, or a tender time to report personnel, certification is essential, which brings a lot of injustice to young people. The emergence of security+ can clear these young people career development obstacles, because security+ emphasis on information security technology, so there is no special requirements for work experience. As long as you have an IT-related background, the pursuit of progress can be studied and tested.
reason two: it operation and maintenance personnel work and turn over the weapon.
in the banking, securities, insurance, information and communications industries, IT operations personnel are very many, it operations involved in the face is also very wide. is a network, system, security, application architecture, storage as one integrated technology post. Although no program ape "born as a Bachelor, Die also write code," The solemn and tragic, but also has "Hoe wo Day Copse, as the operation of suffering" feeling. Every day to the computer and machine, the time has been inevitable for career development confusion and confusion. The advent of security+ international certification allows the pursuit of IT operations personnel to learn network security knowledge, to master network security practices. Career development in the direction of network security, to solve the problem of the shortage of information security personnel in China. In addition, even if not transformation, to do a good job in operation and maintenance, learning safety knowledge to obtain safety certification is also essential.
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/1979864
"Safe Cow Study notes" python Learning Notes