6

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

@mapper
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__
"wrapper"

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?

Joe Halliwell
  • 1,155
  • 6
  • 21
  • I think you need `functools.wraps`. See this: http://stackoverflow.com/questions/308999/what-does-functools-wraps-do/309000#309000 – Pynchia Apr 07 '15 at 09:37
  • @Pynchia: which does exactly the same thing (plus other attribute copies). – Martijn Pieters Apr 07 '15 at 09:42
  • 1
    I think the real question here is *"how can you re`__name__` a generator object?"*; the fact that you want to do so within a decorator is incidental. – jonrsharpe Apr 07 '15 at 09:49
  • Thanks @jonrsharpe, I've amended the title. – Joe Halliwell Apr 07 '15 at 09:53
  • @JoeHalliwell Just wondering why you want to change __name__? – Scott Apr 28 '15 at 15:42
  • 1
    Hi @Scott, **changing** the name of the generator was just a means to an end: that of **preserving** the function name when transforming it via a decorator. (My example was intended to suggest a reasonable use case along those lines.) – Joe Halliwell Apr 29 '15 at 18:37

2 Answers2

8

You only copied across the name to the function. The generator object produced still uses the old name set at compile time.

You would have to set that name each time a new generator object is being produced; unfortunately, the attribute is read-only for generators:

>>> def gen():
...     yield None
... 
>>> gen().__name__
'gen'
>>> gen().__name__ = 'foo'
Traceback (most recent call last):
  File "<stdin>", line 1, in <module>
AttributeError: attribute '__name__' of 'generator' objects is not writable

The name is baked into the code object:

>>> gen.__code__.co_name
'gen'

so the only way you can change this is to re-build the code object (and by extension, the function):

def rename_code_object(func, new_name):
    code_object = func.__code__
    function, code = type(func), type(code_object)
    return function(
        code(
            code_object.co_argcount, code_object.co_nlocals,
            code_object.co_stacksize, code_object.co_flags,
            code_object.co_code, code_object.co_consts,
            code_object.co_names, code_object.co_varnames,
            code_object.co_filename, new_name,
            code_object.co_firstlineno, code_object.co_lnotab,
            code_object.co_freevars, code_object.co_cellvars),
        func.__globals__, new_name, func.__defaults__, func.__closure__)

This creates a new function and code object, using new_name to replace the old name:

>>> new_name = rename_code_object(gen, 'new_name')
>>> new_name.__name__
'new_name'
>>> new_name().__name__
'new_name'
>>> new_name()
<generator object new_name at 0x10075f280>

Using this in your decorator:

def mapper(f):
    def wrapper(items):
        for x in items:
            yield f(x)
    return rename_code_object(wrapper, f.__name__)

Demo:

>>> def mapper(f):
...     def wrapper(items):
...         for x in items:
...             yield f(x)
...     return rename_code_object(wrapper, f.__name__)
... 
>>> @mapper
... def double(x):
...     return x * 2
... 
>>> double([1, 2])
<generator object double at 0x10075f370>
>>> list(double([1, 2]))
[2, 4]

As of Python 3.5, generator objects take their __name__ from the function object rather than from their code object co_name attribute, see issue #21205; the attribute became writable in the process.

Martijn Pieters
  • 1,048,767
  • 296
  • 4,058
  • 3,343
1

Starting with Python 3.5 both the __name__ and __qualname__ attributes are modifiable.

I suggest using functools.wraps() in your decorator, which will automatically assign both of these values to the generator function.

Bharel
  • 23,672
  • 5
  • 40
  • 80