[Python-ideas] aliasing

Mike Meyer mwm at mired.org
Wed Aug 31 20:21:55 CEST 2011


On Wed, Aug 31, 2011 at 5:41 AM, Steven D'Aprano <steve at pearwood.info>wrote:

> To make it work using just ordinary assignment syntax, as you suggest,
> requires more than just an "alias" function. It would need changes to the
> Python internals. Possibly very large changes. Without a convincing
> use-case, I don't think that will happen.
>

Well, once you clear up some of the ambiguities in the request, it's not
quite so bad.

class F(object):
    def m(self):
        alias(a, b)

Which name space is a being aliased in? Function local? Instance? Class?
Module?

So alias needs to take a namespace of some sort. The appropriate object is
easy to find, and should do the trick (though function local might be
entertaining). For simplicity, we'll assume that aliases have to be in the
same namespace (though doing otherwise isn't really harder, just slower).

Passing in variable values doesn't work very well - especially for the new
alias, which may not *have* a value yet! For the name to be aliased, you
could in theory look up the value in the namespace and use the corresponding
name, but there may be more than one of those. So pass that one in as a
string as well.

So make the call "alias(o, name1, name2)". o is a module, class, instance,
or function. Name lookups are done via the __dict__ dict for all of those.
It's easy to write a dict subclass that aliases it's entries.

So the internal changes would be making __dict__ a writable attribute on any
objects which it currently isn't, and making sure that the various objects
use __setitem__ to change attribute values.

I suspect the performance hit - even if you don't use this feature - would
be noticeable. If you used it, it would be even worse.

-1.

     <mike
-------------- next part --------------
An HTML attachment was scrubbed...
URL: <http://mail.python.org/pipermail/python-ideas/attachments/20110831/d313ee84/attachment.html>


More information about the Python-ideas mailing list