How can I use a decorator to modify the type of *args?

2Cubed

I would like to use a @decorator to allow me to be able to access ' - '.join(args) as args, as depicted below. Is this possible, using a metaclass perhaps?

def a(*args):
    print(args)
a(1, 2, 3)
# (1, 2, 3)

@magic
def b(*args):
    print(args)
b(1, 2, 3)
# 1 - 2 - 3
Alex Hall

You can get close:

def magic(func):
    def wrapper(*args):
        return func(' - '.join(map(str, args)))
    return wrapper

but this prints out ('1 - 2 - 3',) because the body of b sees args as a tuple due to the *args, and I doubt a decorator can get around that. What do you expect to happen if the body is something like print(args[1])?

Collected from the Internet

Please contact [email protected] to delete if infringement.

edited at
0

Comments

0 comments
Login to comment

Related

From Dev

How can I modify a type in ack?

From Dev

Python - can I use a decorator for this?

From Dev

How can I create a decorator for a set of models with use the same table?

From Dev

How can I use decorator for functions those have variety parameters?

From Dev

How can I modify Django's login_required decorator on a custom Pinax installation?

From Dev

How Can I Modify The UInteger Type In Visual Basic .NET?

From Dev

how to use decorator pattern to directly modify members of base class?

From Java

how can I use mutate in dplyr to modify variable dynamically?

From Dev

How can I use a sed regex to extract HTML and modify a timestamp?

From Dev

How can I use awk for modify a column based in the first column?

From Dev

How can I modify this code?

From Dev

How can I access args from trait?

From Dev

Can I use the same decorator on a class method and on a static method?

From Dev

How do I modify my Quick Sort Algorithm so that it can sort Double data type arrays?

From Dev

How can I remove a list from another list using a field value of the element's type if i can't modify the class directly?

From Dev

how to use decorator in a class

From Dev

How can I use a macro with refined return type inline?

From Dev

How can I use same methods in different classes type ArrayList?

From Java

How can I use typescript to destructure from a complex type?

From Dev

What is the Addr# type, and how can I use it?

From Dev

how can I use PostgreSQL's "text" column type in Django?

From Dev

How can I use a type synonym in an instance declaration?

From Dev

How can I know which type of process of linux will use for these?

From Dev

How can I use True Type Collection (.ttc) in JavaFX?

From Dev

How can I use the pattern matching result in the List of matching type?

From Dev

How can i use oracle object type in stored procedure?

From Dev

How can I use a Class object as a parameterized type?

From Dev

How can I use a type reference in a XAML / Universal Windows app?

From Dev

How can I use Scala reflection to find the self type traits?

Related Related

  1. 1

    How can I modify a type in ack?

  2. 2

    Python - can I use a decorator for this?

  3. 3

    How can I create a decorator for a set of models with use the same table?

  4. 4

    How can I use decorator for functions those have variety parameters?

  5. 5

    How can I modify Django's login_required decorator on a custom Pinax installation?

  6. 6

    How Can I Modify The UInteger Type In Visual Basic .NET?

  7. 7

    how to use decorator pattern to directly modify members of base class?

  8. 8

    how can I use mutate in dplyr to modify variable dynamically?

  9. 9

    How can I use a sed regex to extract HTML and modify a timestamp?

  10. 10

    How can I use awk for modify a column based in the first column?

  11. 11

    How can I modify this code?

  12. 12

    How can I access args from trait?

  13. 13

    Can I use the same decorator on a class method and on a static method?

  14. 14

    How do I modify my Quick Sort Algorithm so that it can sort Double data type arrays?

  15. 15

    How can I remove a list from another list using a field value of the element's type if i can't modify the class directly?

  16. 16

    how to use decorator in a class

  17. 17

    How can I use a macro with refined return type inline?

  18. 18

    How can I use same methods in different classes type ArrayList?

  19. 19

    How can I use typescript to destructure from a complex type?

  20. 20

    What is the Addr# type, and how can I use it?

  21. 21

    how can I use PostgreSQL's "text" column type in Django?

  22. 22

    How can I use a type synonym in an instance declaration?

  23. 23

    How can I know which type of process of linux will use for these?

  24. 24

    How can I use True Type Collection (.ttc) in JavaFX?

  25. 25

    How can I use the pattern matching result in the List of matching type?

  26. 26

    How can i use oracle object type in stored procedure?

  27. 27

    How can I use a Class object as a parameterized type?

  28. 28

    How can I use a type reference in a XAML / Universal Windows app?

  29. 29

    How can I use Scala reflection to find the self type traits?

HotTag

Archive