简体   繁体   中英

Python code to create function with Reversed arguments

How to write a function that returns the reversed arguments of a previous function? The function can return an int, char, string, etc. Examples: f=pow(2,3)=8 (original) g=pow(3,2)=9 (reversed arguments)

capitalize_first_and_join first second third = FIRSTsecondthird (original) capitalize_first_and_join first second third = THIRDsecondfirst (reversed arguments)

join_with coder best the are you = coder,best,the,are,you (original) join_with coder best the are you = you,are,the,best,coder (reversed arguments)

The name of the function I need to create is reversed_args(f)

Regards,

def reversed_args(f):
    # create the function here

    int_func_map = {
    'pow':pow,
    'cmp':cmp,
    }

    string_func_map ={
    'join with': lambda separator, *args: separator.join(*args),
    'capitalize_first_and_join': lambda first, *args: ''.join([first.upper()] + list(args)),
    }

    queries = int(raw_input())
    for _ in range (queries):
    line = raw_input().split()
    func_name, args = line[0], line[1:]
    if func_name in int_func_map:
        args = map(int, args)
        print reversed_args(int_func_map[func_name])(*args)
    else:
        print reversed_args(string_func_map[func_name](*args)




    f=pow(2,3)=8 (original)
    g=pow(3,2)=9 (reversed arguments)

    capitalize_first_and_join first second third = FIRSTsecondthird (original)
    capitalize_first_and_join first second third = THIRDsecondfirst (reversed arguments)

    join_with coder best the are you = coder,best,the,are,you (original)
    join_with coder best the are you = you,are,the,best,coder (reversed arguments)

this is code should help:

from functools import wraps

def reversed_args(f):
    @wraps(f)
    def g(*args):
        return f(*args[::-1])
    return g

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM