anything like C++ references?

Tom Plunket tomas at fancy.org
Mon Jul 14 20:48:31 EDT 2003


sismex01 wrote:

> > What I specifically want is a way to have a "second-level
> > binding", e.g. a reference to a reference (assuming that the
> > terminology is correct by saying that all Python variables are
> > references).
> 
> What for?  What would be the practical use for such a thing?

"Why would you want a bike when you have a car?"

I would say that I wanted it because I thought it would be the
most straight-forward solution to the problem, so I thought I'd
ask.

I saw the weakref docs, but it didn't seem to do what I expected
it to do, so I asked the question.  ;)  At this point, I'm not
entirely sure what use weakref has, since it seems to only
provide the same mechanism that exists already through
assignment.

> To rebind a name remotely?  It's not very consistent with the
> rest of the language.

hmm.

def fn(b): b.val = 4

class MyClass: pass

a = MyClass()
a.val = 3
fn(a)
print a.val

This name was rebound remotely.  I'm not trying to be difficult,
but this does seem like much the same thing if not a bit more
heavy-weight than what I'm asking about.

> To have functions which rebind a name in a hidden way seems
> to me like a veritable fountain of possible bugs.  Explicit
> is better than implicit and all that.

and OO will never take hold, either.  ;)  Just look at all of the
behind-the-scenes that's going on!

> Those operations might have sense in a language where a variable
> name is directly related to a storage location, a memory address;
> but Python is not like that, and trying to program in that style
> in Python is quite painful.

That's why I'm not trying to program in that style, but in a new
style that seems to me to be in the Python way, if not (yet? <g>)
supported by the language.

> I find Python to be quite clear and powerful, and never have needed
> such functionality in all the different type of projects I've done,
> small-to-medium, but varied: SQL parsers, XML parsers, email
> messaging systems, GUI frontends, RPC servers and clients,
> text-formatting utilities, etc...  I've never found a single
> instance where I would have needed such that.

I've never needed the subtraction operator, myself.  Every time I
used it, I could have used a negate and an add, but I used the
subtraction operator instead.

Just because functionality isn't strictly needed doesn't mean
it's not useful.


-tom!




More information about the Python-list mailing list