a fairly ugly/kludgy way to get 'aliases' in Python

Jonathan P. jbperez808 at yahoo.com
Tue Jan 21 15:57:25 CET 2003


**** Post for FREE via your newsreader at post.usenet.com ****

> (Except I think it was a typo and either his alias1
> was supposed to be alias or vice versa).

Whoops, yes it was a typo.

> If you want to do table-driven aliasing in another way, look at the x method below, yet note
> that the actual values are stored with the long-name attributes (as seen with vars(a)).
> Of course, I'm not sure what all this is about ;-)

I'd like to be able to substitute a shorter alias
for a member variable with a long name at certain
arbitrary points in the source code when using
self.long_long_name multiple times would quickly
cause code to be unreadable and/or need multiple
lines to contain.

In certain contexts it makes sense to use a shorter
alias for a member variable as in below:

    height=self.numDisplayedItems # unassignable alias of self.numDisplayedItems
    halfway=height/2
    firstItemToShow=min(max((self.selectedIndex>halfway)
                        *self.selectedIndex-halfway,0),len(self.list)-height)
    for item in self.list[firstItemToShow:firstItemToShow+height]:
      SLsmg_gotorc(y,x)
     ...

Here I want to name the variable referring to the number of
items a dropdown list can show at one time as numDisplayedItems
because I find it a more accurate description.  But in certain
parts of the code like the one shown above, I'd like to refer
to it by the variable name 'height' because in the context of
the calculations shown, it makes sense to call it that and
being shorter, it would also make the code a lot more
readable.  Unassignable RHS-only aliases probably would do in
majority of the cases, but I was also trying to think up of a
way to make an alias work on the left hand side.


> ===< t_alias.py >==============
> class Aliaser(type):
>     def __new__(cls, name, bases, cdict):
>         for alias, orig in cdict['__rw_aliases__']:
>             d = {'alias':alias, 'orig':orig}
>             exec ("""\
> def _get_%(alias)s(self): return self.%(orig)s
> def _set_%(alias)s(self, v): self.%(orig)s = v
> %(alias)s = property(_get_%(alias)s, _set_%(alias)s)
> """ % d) in cdict
>         return type.__new__(cls, name, bases, cdict)

Some interesting Python voodoo there worthy of study.
Thanks.



-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=
 *** Usenet.com - The #1 Usenet Newsgroup Service on The Planet! ***
                      http://www.usenet.com
Unlimited Download - 19 Seperate Servers - 90,000 groups - Uncensored
-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=




More information about the Python-list mailing list