Search
 
SCRIPT & CODE EXAMPLE
 
CODE EXAMPLE FOR PYTHON

python decorator generator to list

def listify(gen):
    "Convert a generator into a function which returns a list"
    def patched(*args, **kwargs):
        return list(gen(*args, **kwargs))
    return patched

@listify
def f(x):
     for i in range(x):
        yield "item" + str(i)

assert f(5) == "item0 item1 item2 item3 item4".split()
Source by argandgahandapandpa.wordpress.com #
 
PREVIOUS NEXT
Tagged: #python #decorator #generator #list
ADD COMMENT
Topic
Name
8+3 =