How to use say_hello method in assertpy

Best Python code snippet using assertpy_python

demo01.py

Source:demo01.py Github

copy

Full Screen

2 装饰器3"""4# 需求变化:在以下两个函数中,增加新功能(在控制台中打印函数名称).5"""6def say_hello(): 7 print("hello")8def say_goodbye(): 9 print("goodbye")10say_hello()11say_goodbye()12"""13# 缺点:新增加的功能,定义了多次.14"""15def say_hello():16 print("say_hello")17 print("hello")18def say_goodbye():19 print("say_goodbye")20 print("goodbye")21say_hello()22say_goodbye()23"""24# 行为相同,数据不同.25# 提取相同的行为26# 缺点: 增加新功能,修改原有函数内部(代码可读性不高).27"""28def print_func_name(func):29 print(func.__name__)# 函数 --> 名称30def say_hello():31 print_func_name(say_hello)32 print("hello")33def say_goodbye():34 print_func_name(say_goodbye)35 print("goodbye")36"""37"""38# 新功能39def print_func_name(func):40 print(func.__name__)41# 旧功能42def say_hello(): 43 print("hello")44def say_goodbye(): 45 print("goodbye")46# 旧功能 = 新功能 + 旧功能47say_hello = print_func_name + say_hello48 49say_hello()50say_goodbye()51"""52# 缺点:定义完旧功能,需要在旧功能下面用内部函数覆盖.53"""54def print_func_name(func): # func --> say_hello55 def wrapper():56 # 定义新功能57 print(func.__name__)58 # 调用旧功能59 func()60 return wrapper61# 旧功能62def say_hello():63 print("hello")64def say_goodbye():65 print("goodbye")66# 旧功能 = 新功能 + 旧功能67# 旧功能 = 新功能 ( 旧功能 ) # 返回值是内部函数(新功能 + 旧功能)68say_hello = print_func_name(say_hello)69say_goodbye = print_func_name(say_goodbye)70say_hello()# 调用内部函数71say_goodbye()72"""73# 缺点:如果参数不同,会异常.74"""75def print_func_name(func): # func --> say_hello76 def wrapper():77 # 定义新功能78 print(func.__name__)79 # 调用旧功能80 func()81 return wrapper82# 旧功能83@print_func_name # say_hello = print_func_name(say_hello)84def say_hello():85 print("hello")86@print_func_name # say_goodbye = print_func_name(say_goodbye)87def say_goodbye():88 print("goodbye")89say_hello()# 调用内部函数90say_goodbye()91"""92# 缺点:旧功能的返回值丢失了93"""94def print_func_name(func): # func --> say_hello95 def wrapper(*args):# 星号元组形参96 # 定义新功能97 print(func.__name__)98 # 调用旧功能99 func(*args)# 序列实参100 return wrapper101# 旧功能102@print_func_name# say_hello = print_func_name(say_hello)103def say_hello():104 print("hello")105 return 1106@print_func_name# say_goodbye = print_func_name(say_goodbye)107def say_goodbye(name):108 print(name,"goodbye")109 return 2110print(say_hello())# 调用内部函数111print(say_goodbye("qtx"))112"""113def print_func_name(func): # func --> say_hello114 def wrapper(*args,**kwargs):115 # 定义新功能116 print(func.__name__)117 # 调用旧功能118 return func(*args,**kwargs)119 return wrapper120# 旧功能121@print_func_name# say_hello = print_func_name(say_hello)122def say_hello():123 print("hello")124 return 1125@print_func_name# say_goodbye = print_func_name(say_goodbye)126def say_goodbye(name):127 print(name,"goodbye")128 return 2129print(say_hello())# 调用内部函数...

Full Screen

Full Screen

code01.py

Source:code01.py Github

copy

Full Screen

1"""2装饰器3"""4"""5def say_hello():6 print("hello")7def say_goodbye():8 print("goodbye")9say_hello()10say_goodbye()11"""12# 需求:在两个方法所实现的功能基础上,增加一种新功能(打印方法名称).13"""14def say_hello():15 print(say_hello.__name__)16 print("hello")17def say_goodbye():18 print(say_goodbye.__name__)19 print("goodbye")20"""21# 在say_hello与say_goodbye内部调用新功能,不容易改变(增加/删除)22"""23def print_func_name(func):24 print(func.__name__)25def say_hello():26 print_func_name(say_hello)27 print("hello")28def say_goodbye():29 print_func_name(say_goodbye)30 print("goodbye")31"""32"""33def print_func_name(func):# 提供旧功能34 def wrapper():# 包装35 print(func.__name__)# 新功能36 func()# 调用旧功能37 return wrapper38def say_hello():39 print("hello")40def say_goodbye():41 print("goodbye")42# 调用外部函数43say_hello = print_func_name(say_hello)44say_goodbye = print_func_name(say_goodbye)45# 调用内部函数(包装新 + 旧功能)46say_hello()47say_goodbye()48"""49"""50def print_func_name(func):# 提供旧功能51 def wrapper():# 包装52 print(func.__name__)# 新功能53 func()# 旧功能54 return wrapper55@print_func_name # say_hello = print_func_name(say_hello)56def say_hello():57 print("hello")58@print_func_name59def say_goodbye():60 print("goodbye")61say_hello()62say_goodbye()63"""64def print_func_name(func): # 提供旧功能65 def wrapper(*args, **kwargs): # 包装66 print(func.__name__) # 新功能67 return func(*args, **kwargs) # 旧功能68 return wrapper69@print_func_name # say_hello = print_func_name(say_hello)70def say_hello():71 print("hello")72 return "ok"73@print_func_name74def say_goodbye(name):75 print(name, "goodbye")76result = say_hello()77print(result)...

Full Screen

Full Screen

Automation Testing Tutorials

Learn to execute automation testing from scratch with LambdaTest Learning Hub. Right from setting up the prerequisites to run your first automation test, to following best practices and diving deeper into advanced test scenarios. LambdaTest Learning Hubs compile a list of step-by-step guides to help you be proficient with different test automation frameworks i.e. Selenium, Cypress, TestNG etc.

LambdaTest Learning Hubs:

YouTube

You could also refer to video tutorials over LambdaTest YouTube channel to get step by step demonstration from industry experts.

Run assertpy automation tests on LambdaTest cloud grid

Perform automation testing on 3000+ real desktop and mobile devices online.

Try LambdaTest Now !!

Get 100 minutes of automation test minutes FREE!!

Next-Gen App & Browser Testing Cloud

Was this article helpful?

Helpful

NotHelpful