__name__ becoming read-write?

Anthony Baxter anthonybaxter at gmail.com
Tue Aug 24 06:27:15 CEST 2004

On Mon, 23 Aug 2004 23:17:25 GMT, Arthur <ajsiegel at optonline.com> wrote:
> > "So wait, this stuff that _looks_ like code in a
> >function, isn't actually? what the hell?" The new syntax[1] for
> >decorators is at least very obvious that something _new_ is going on.
> I am thinking (and I think Paul is thinking) that we can say the same
> thing, succintly, in a manner that has precedence in the language
> Mine's a one-liner that does not totally relieve the magicians burden
> to be - somwhere down the line -  a bit expressive

I'm not _quite_ sure what it is you're asking for here, but it _seems_
like you want to have two different blocks inside a def - the first is
the meta-block, which contains docstrings and other magic attributes,
while the second is the actual code body? Is this correct? If this is
the case, I think it's _possible_ that in the future we might see
something like this, for typing purposes. Maybe. I'm not sure. But if
we do, I can't see any point _at_ _all_ to signifying magic things
with arbitrary words surrounded with __under__ meaning 'under is a
decorator'. While Python does use __foo__, the list of values for foo
that are meaningful is well described and documented.

As to the specific detail about whether you can assign to a functions
__name__ from inside the function - this will break a lot of tools
that attempt to handle python code. How is an editor to find the
method 'frobulate' in a .py file? Right now, it can be done with a
pretty simple regexp. Even if one of the syntaxes for decorators that
insert the decorators on the def line goes in, you can still do it
with a more complex regexp. But something like this?

class Frobozz:
  def frobulate():
                 __name__ = defrobulate

An IDE or editor that tries to find defrobulate is going to go insane.

One final clarification - altering a method's __name__ does not change
it's name in the class's __dict__.

>>> class A:
...     def foo(self): pass
...     foo.__name__ = 'bar'
>>> A().bar()
Traceback (most recent call last):
  File "<stdin>", line 1, in ?
AttributeError: A instance has no attribute 'bar'
>>> A().foo()

More information about the Python-list mailing list