Featured Post

Best Practices for Handling Duplicate Elements in Python Lists

Image
Here are three awesome ways that you can use to remove duplicates in a list. These are helpful in resolving your data analytics solutions.  01. Using a Set Convert the list into a set , which automatically removes duplicates due to its unique element nature, and then convert the set back to a list. Solution: original_list = [2, 4, 6, 2, 8, 6, 10] unique_list = list(set(original_list)) 02. Using a Loop Iterate through the original list and append elements to a new list only if they haven't been added before. Solution: original_list = [2, 4, 6, 2, 8, 6, 10] unique_list = [] for item in original_list:     if item not in unique_list:         unique_list.append(item) 03. Using List Comprehension Create a new list using a list comprehension that includes only the elements not already present in the new list. Solution: original_list = [2, 4, 6, 2, 8, 6, 10] unique_list = [] [unique_list.append(item) for item in original_list if item not in unique_list] All three methods will result in uni

Python Function Argument: How to Pass it to Decorator

A decorator is a wrapper and provides additional functionality to a function. Also, it may modify the behavior, such as changing the return type/adding new abilities.


Python Decorators



Python Decorators


Precisely, it is another form of function pointers. Also, it accepts function argument, then either wraps the function or returns a new one. Moreover, it modifies the inputs/outputs supplied to it. It helps you add behavior to functions (objects) dynamically (without changing the function behavior).

Function Argument

Below, you will find an example of passing a function argument to a decorator. The below function modifies inputs and returns output.

def to_upper(func):
    text=func()
    if isinstance(text,str):
        return text.upper()

def say():
    return "welcome"

def hello():
    return "hello"
    
a = to_upper(say)
print(a) 

b = to_upper(hello)
print(b)  


Output


WELCOME
HELLO


** Process exited - Return Code: 0 **
Press Enter to exit terminal

References

Comments

Popular posts from this blog

Explained Ideal Structure of Python Class

6 Python file Methods Real Usage

How to Decode TLV Quickly