...so would aliases be a bad idea? (Re: references / aliases in Python)

Jonathan P. jbperez808 at yahoo.com
Wed Jan 15 19:25:54 EST 2003


> The Python philosophy regarding poiner types is simple: there aren't any.
> Names are references to values. Attributes of objects are references to
> values. Elements of dictionaries, lists and tuples are references to values.
> Values do not have names, they are simply bound by assignment (to names,
> attributes of objects, or elements of dictionaries, lists and tuples). Each
> value (object) has an associated reference counter, incremented and
> decremented automatically as references are created and destroyed. When an
> object's reference count reaches zero it becomes a candidate for garbage
> collection. Special features allow collection of "cyclic garbage" to handle
> the cases where a set of objects refer only to each other.

So if one allows 2 different names to bind to the exact same object,
would that be a bad idea?  There are many situations where one
would like a shorter name to bind to an object which might otherwise
be only referrable to only by a very long chain of attributes.

>>> class A:
      def __init__(self):
        self.long_name1=5
        self.long_name2=15
      def f1(self):
        alias1=&self.long_descriptive_name1  # alias1 and alias2 become
        alias2=&self.long_descriptive_name2  # bound to the same objects as 
        if alias1>alias2+1: alias1=alias2    # self.long_descriptive_name1 and
                                             # self.long_descriptive_name2
                                             # respectively

the last 3 lines replacing the long and rather unreadable 
(in practice it often gets much worse than shown below):

      if self.long_descriptive_name1>self.long_descriptive_name2+1:
        self.long_descriptive_name1=self.long_descriptive_name2

If one objects to the & operator to generate an alias, one could always 
use a function: 

anAlias=alias(self.long_var_name_bound_to_integer)

More comments...?




More information about the Python-list mailing list