最新消息:雨落星辰是一个专注网站SEO优化、网站SEO诊断、搜索引擎研究、网络营销推广、网站策划运营及站长类的自媒体原创博客

How do i execute the python function using decorator metadata without calling original function. is it possible? - Stack Overflo

programmeradmin1浏览0评论

here is my python code i am trying. I am expecting Stored Metadata list. Memory Decorator Purpose is the code where functions are "decorated" to store metadata.

import functools

memory_storage = []

def memory(func_names, description):
    def decorator(func):
        @functools.wraps(func)
        def wrapper(*args, **kwargs):
            # Store the metadata before function execution
            memory_storage.append({
                "func_names": func_names,
                "description": description,
                "function": func
            })
            return func(*args, **kwargs)
        return wrapper
    return decorator

@memory(
    func_names=['print_weep'],
    description="This method will print the weep"
)
def print_weep():
    print("weep")

@memory(
    func_names=['print_hello'],
    description="This method will print hello"
)
def print_hello():
    print("hello")

def execute_functions():
    for entry in memory_storage:
        print(f"Executing function {entry['func_names'][0]}: {entry['description']}")
        entry['function']()  # Call the stored function

print("Stored Metadata:", memory_storage)
execute_functions()

here is my python code i am trying. I am expecting Stored Metadata list. Memory Decorator Purpose is the code where functions are "decorated" to store metadata.

import functools

memory_storage = []

def memory(func_names, description):
    def decorator(func):
        @functools.wraps(func)
        def wrapper(*args, **kwargs):
            # Store the metadata before function execution
            memory_storage.append({
                "func_names": func_names,
                "description": description,
                "function": func
            })
            return func(*args, **kwargs)
        return wrapper
    return decorator

@memory(
    func_names=['print_weep'],
    description="This method will print the weep"
)
def print_weep():
    print("weep")

@memory(
    func_names=['print_hello'],
    description="This method will print hello"
)
def print_hello():
    print("hello")

def execute_functions():
    for entry in memory_storage:
        print(f"Executing function {entry['func_names'][0]}: {entry['description']}")
        entry['function']()  # Call the stored function

print("Stored Metadata:", memory_storage)
execute_functions()
Share Improve this question edited Feb 2 at 6:52 chinna asked Feb 2 at 6:44 chinnachinna 192 bronze badges 3
  • So you expect memory to be triggered without triggering print_weep and print_weep? – Guy Commented Feb 2 at 6:53
  • yes i am expecting memory to be triggered without triggering print_weep and print_hello – chinna Commented Feb 2 at 6:57
  • 3 A decorator is called only when you call the function it decorates. What is your end goal here, why do you want to do it? – Guy Commented Feb 2 at 7:07
Add a comment  | 

1 Answer 1

Reset to default 1

I'm taking a guess that you want to store the functions and call them by iterating through the memory_storage list. If that's the case you are putting the append call in the wrong function: the memory would be appended when the print_weep/print_hello functions are called, not when the decorator is applied. Try this:

import functools

memory_storage = []

def memory(func_names, description):
    def decorator(func):
        # This should be put here
        memory_storage.append({
            "func_names": func_names,
            "description": description,
            "function": func
        })
        @functools.wraps(func)
        def wrapper(*args, **kwargs):
            return func(*args, **kwargs)
        return wrapper
    return decorator

@memory(
    func_names=['print_weep'],
    description="This method will print the weep"
)
def print_weep():
    print("weep")

@memory(
    func_names=['print_hello'],
    description="This method will print hello"
)
def print_hello():
    print("hello")

def execute_functions():
    for entry in memory_storage:
        print(f"Executing function {entry['func_names'][0]}: {entry['description']}")
        entry['function']()  # Call the stored function

print("Stored Metadata:", memory_storage)
execute_functions()

Output:

Stored Metadata: [{'func_names': ['print_weep'], 'description': 'This method will print the weep', 'function': <function print_weep at 0x1007432e0>}, {'func_names': ['print_hello'], 'description': 'This method will print hello', 'function': <function print_hello at 0x100743920>}]
Executing function print_weep: This method will print the weep
weep
Executing function print_hello: This method will print hello
hello

与本文相关的文章

发布评论

评论列表(0)

  1. 暂无评论