简体   繁体   English

如何将我的 function 更改为装饰器 python?

[英]how i can change my function into decorator python?

in this code i checking the email and password validation在此代码中,我检查 email 和密码验证

if email ends with {@gmail.com} and password length is 8 i print (hello user)如果 email 以 {@gmail.com} 结尾并且密码长度为 8 i print (hello user)

def login(email, password):
   valid_mail = "@gmail.com"
   print()
   if email[-10:] == valid_mail and len(str(password)) == 8:
       print(f'hello  {email} welcome back')
   else:
       print("invalid user")

now i want to change my login function to现在我想将我的登录 function 更改为

def login(email, password):
  print(f' welcome {email }')

and with decorator function checking the condition if its true then print login function,并使用装饰器 function 检查条件是否为真,然后打印登录 function,

def my_decorator(func):
    def wrapper_function(*args, **kwargs):
        if email[-10:] == "@gmail.com" and len(str(password)) == 8:
            return wrapper_function
        else:
            print("not user")
        return func(*args, **kwargs)

    return wrapper_function

i know it's wrong solution, i just learning python and a little confused ) please help me我知道这是错误的解决方案,我刚刚学习 python 有点困惑)请帮助我

>>>
>>>
>>> email = ' example@gmail.com   '
>>> email.endswith('@gmail.com')
False
>>>
>>> email.strip()
'example@gmail.com'
>>>
>>> email.strip().endswith('@gmail.com')
True
>>>


def login(email):
    user = email.split('@')[0]
    print('hello ',user)

def my_decorator(email,password):
    email = email.strip()


    if email.endswith('@gmail.com') and len(password) == 8:
        login(email)
    else:
        print("invalid user")
    
my_decorator('example_@gmail.com','@1234567')

my_decorator('example@gmail.com','123456789')

A decorator can be implemented as a function with an inner function that act as a wrapper.装饰器可以实现为 function ,内部 function 充当包装器。 The decorator takes in a function, wraps it, calls the input function, then returns the wrapper:装饰器接收一个 function,包装它,调用输入 function,然后返回包装器:

from functools import wraps


def my_decorator(func):
    
    @wraps(func)
    def wrapper_function(*args, **kwargs):
        email = kwargs.get("email", "")
        password = kwargs.get("password", "")
        
        if email[-10:] == "@gmail.com" and len(str(password)) == 8:
            func(**kwargs)
        else:
            print("not user")
            
    return wrapper_function

@my_decorator
def login(email, password):
    print(f' welcome {email}')


login(email="johnsmith@gmail.com", password="01234567")

You may also want to take a look at functools.wraps , which is a decorator itself and how it avoids replacing the name and docstring of the decorated function with the one of the wrapper.您可能还想看看functools.wraps ,它本身就是一个装饰器,以及它如何避免用包装器之一替换装饰的 function 的名称和文档字符串。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

相关问题 如何在python装饰器中以编程方式更改函数*不*的argspec? - How can I programmatically change the argspec of a function *not* in a python decorator? 如何以编程方式更改python装饰器中函数的argspec? - How can I programmatically change the argspec of a function in a python decorator? python 装饰器如何更改装饰 function 中的调用? - how can a python decorator change calls in decorated function? 在Python中,如何获取带有参数传递给我的装饰器的函数的名称? - In Python, how can I get the name of the function passed to my decorator with parameters? 如何获取在python中传递给我的装饰器的函数的文件名? - How can I get the file name of the function that is passed to my decorator in python? 我怎样才能成功地使用装饰器函数来解决我的问题? - How can I successfully use a decorator function for my problem? 如何将结果从 python 装饰器传递给我的类? - How can I pass the result from a python decorator to my class? 如何在 Python 中为这个简单的函数编写装饰器? - How can I write a decorator for this simple function in Python? Python:如何使用装饰器向 function 添加 doxygen 注释? - Python: How can I add a doxygen comment to a function with a decorator? 如何在python中使用装饰器更改函数的返回值? - How change a function's return with decorator in python?
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM