...so would aliases be a bad idea? (Re: references / aliases in Python)
Jp Calderone
exarkun at intarweb.us
Wed Jan 15 19:40:36 EST 2003
On Wed, Jan 15, 2003 at 04:25:54PM -0800, Jonathan P. wrote:
> > 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
Anything that might cause confusing, unexpected behavior just for the sake
of saving a few keystrokes is bad idea in my book. A much better solution
to this problem, for me, would be...
class A:
# long, descriptive text, or better, property() with a docstring
n1 = n2 = None
def __init__(self):
self.n1 = 5
self.n2 = 15
def f1(self):
if self.n1 > self.n2: self.n1 = self.n2
> [snip]
>
> More comments...?
If you consider the potential for seriously non-local effects that this
opens up, I don't see how one can see it is a plus :) Imagine if a library
took an "alias" from something you passed in to it. Even if this was well
documented, it would be hell to track down if you weren't expecting it.
Jp
--
Lowery's Law:
If it jams -- force it. If it breaks, it needed replacing anyway.
--
12:00am up 30 days, 9:47, 3 users, load average: 0.39, 0.25, 0.25
-------------- next part --------------
A non-text attachment was scrubbed...
Name: not available
Type: application/pgp-signature
Size: 196 bytes
Desc: not available
URL: <http://mail.python.org/pipermail/python-list/attachments/20030115/5300727c/attachment.sig>
More information about the Python-list
mailing list