Changing The Name Of A Generator

    Table of contents

Changing the __name__ of a generator

Tags: python , generator , python-2.7 Answers: 1 | Viewed 523 times

Given the following setup:

def mapper(f):
def wrapper(items):
for x in items:
yield f(x)
wrapper.__name__ = f.__name__ # This has no effect!
return wrapper
def double(x):
return x * 2

The decorator works as expected:

>>> [x for x in double([1,2,3])]
[2, 4, 6]

However its __name__ is not double as desired:

>>> double([1,2]).__name__

Is it possible to force the name of the generator?
Alternatively, is it possible to dig around in the generator object and retrieve the name double?

Some Answers For Changing The Name Of A Generator

Please leave your answer here: