1. Local Variables
Inside a function, you can explicitly declare a global variable with global. It's never going to work this way.
CTRL +? Multiline Comment I
2. Advanced functions
Pass the function name as a parameter to the function
def add (a,b,f):
Return f (a) +f (b)
res = Add (3,-6,abs)
ABS is a built-in function
def bar ():
Print ("In the Bar")
def test1 (func):
First look at the first example:
def bar ():
Print ("In the Bar")
def test1 (func):
Print (func)
Func ()
Test1 (BAR)
<function Bar at 0x0000019a43bc2e18>
In the bar
Next look at the second example:
def bar ():
Time.sleep (3)
Print ("In the Bar")
def test1 (func):
Start_time = Time.time ()
Func ()
Stop_time = Time.time ()
Print ("The Func Run Time is:%s"% (Stop_time-start_time))
Test1 (BAR)
In the bar
The Func run time is:3.000192880630493
It can be seen that the bar () is not modified under the source code to add functionality to it
Finally, a third example:
def bar ():
Print ("In the Bar")
def test1 (func):
Print (func)
return func
Print (Test1 (bar))
Test1 (bar) ()
Bar = test1 (bar)
Bar ()
<function Bar at 0x000002347ace2e18>
<function Bar at 0x000002347ace2e18>
<function Bar at 0x000002347ace2e18>
In the bar
<function Bar at 0x000002347ace2e18>
In the bar
2. Anonymous functions:
Lambda x:x*3
Calc = Lambda x:x*3
Calc (3)
3. Reference counting
Python reclaims memory by reference count
4. Nesting functions
In a function body def another function
def foo ():
Print ("In the Foo")
def bar ():
Print ("int the Bar")
Def Love ():
Print ("int the Love")
Love ()
Bar ()
Foo ()
5. The adorner is essentially a function that decorates other functions and adds additional functionality to other functions. Grammatical sugars.
1. Cannot modify the source code of the decorated function
2. Cannot modify the calling mode of the decorated function
Become transparent
Realize:
The 1 function is a "variable" that defines the re-use
2. Advanced functions
3. Nesting functions
Import time
def timer (func):
Def deco ():
Start_time = Time.time ()
Func ()
Stop_time = Time.time ()
Print ("The Func Run Time is:%s"% (Stop_time-start_time))
Return deco
def test1 ():
Time.sleep (3)
Print ("in teh tes1")
Def test2 ():
Time.sleep (3)
Print ("in teh tes2")
Test1=timer (test1)
Test1 ()
In teh tes1
The Func run time is:3.0001118183135986
The end result is:
Import time
def timer (func):
Def deco ():
Start_time = Time.time ()
Func ()
Stop_time = Time.time ()
Print ("The Func Run Time is:%s"% (Stop_time-start_time))
Return deco
@timer #test1 =timer (test1)
def test1 ():
Time.sleep (3)
Print ("in teh tes1")
@timer
Def test2 ():
Time.sleep (3)
Print ("in teh tes2")
Test1 ()
Most ultimately: Import time
def timer (func):
def deco (*args,**kvargs):
Start_time = Time.time ()
Func (*args,**kvargs)
Stop_time = Time.time ()
Print ("The Func Run Time is:%s"% (Stop_time-start_time))
Return deco
@timer
def test1 ():
Time.sleep (3)
Print ("In the Test1")
@timer
def test2 (name,age):
Time.sleep (3)
Print ("In the Test", Name,age)
Test1 ()
Test2 ("Roger", 22)
Add:
Username,password = "Roger", "Abc123"
def auth (func):
def wrapper (*args,**kvargs):
Username = input ("username:")
Password = Input ("Password:")
if username = = "Roger" and Password = = "abc123":
Print ("User has passed auth Enticated ")
Func (*args,**kvargs)
Else:
Print (" Invalid username or password ")
Exit ()
return wrapper
Def index ():
Print ("Welcome to Index page")
@auth
def home (): br> print ("Welcome to Home Page")
@auth
def bbs ():
Print ("Welcome to BBS page")
Index ()
Home () bbs ()
Continue:
Username,password = "Roger", "Abc123"
def auth (func):
def wrapper (*args,**kvargs):
Username = input ("Username:")
Password = input ("Password:")
If username = = "Roger" and Password = = "Abc123":
Print ("User has passed authenticated")
ResU = func (*args,**kvargs)
Print ("After authentication")
Return ResU
Else
Print ("Invalid username or password")
Exit ()
Return wrapper
def index ():
Print ("Welcome to Index page")
@auth
Def home ():
Print ("Welcome to Home Page")
Return "From Home"
@auth
Def BBS ():
Print ("Welcome to BBS page")
Index ()
Print (Home ())
BBS ()
To continue:
Username,password = "Roger", "Abc123"
def auth (auth_type):
Print ("auth function args", auth_type)
def out_wrapper (func):
def wrapper (*args, **kvargs):
Print ("wrapper function args:", *args,*kvargs)
if Auth_type = = "Local":
Username = input ("Username:")
Password = input ("Password:")
If username = = "Roger" and Password = = "Abc123":
Print ("User has passed authenticated")
ResU = Func (*args, **kvargs)
Print ("After authentication")
Return ResU
Else
Print ("Invalid username or password")
Exit ()
elif Auth_type = = "LDAP":
Print ("Authentication to perform LDAP")
Return wrapper
Return Out_wrapper
def index ():
Print ("Welcome to Index page")
@auth (auth_type= "local")
Def home ():
Print ("Welcome to Home Page")
Return "From Home"
@auth (auth_type= "LDAP")
Def BBS ():
Print ("Welcome to BBS page")
Index ()
Print (Home ())
BBS ()
Python local variables, advanced functions, anonymous functions, nested functions, adorners