[Tutor] Still confused about Python references/objects

Remco Gerlich scarblac@pino.selwerd.nl
Sun, 1 Apr 2001 23:19:26 +0200


On Sun, Apr 01, 2001 at 02:08:05PM -0700, Sheila King wrote:
> On Sun, 1 Apr 2001 21:06:23 +0200, Remco Gerlich <scarblac@pino.selwerd.nl>
> wrote about Re: [Tutor] Still confused about Python references/objects:
> 
> :On Sat, Mar 31, 2001 at 04:20:12PM -0800, Sheila King wrote:
> :> On Sat, 31 Mar 2001 14:52:00 -0500, "Tim Peters" <tim.one@home.com>  wrote
> :> about RE: [Tutor] Still confused about Python references/objects:
> :> 
> :> :"by reference" isn't really a useful concept in Python.  People bring that
> :> :with them from other languages, and get into all sorts of trouble trying to
> :> :force it to fit.
> :> 
> :> You're telling me! (I bring background experience from a few different
> :> languages: Pascal, Fortran, VB, C++, and they ALL behaved that way! It is so
> :> difficult to get used to this new way of thinking.)
> :
> :This point may be hard to get at first, but luckily it's one of the few
> :things!
> :
> :I think once you understand:
> :1 Everything is by reference
> 
> No, your point number one is not really useful (at least for me). I had read
> remarks like that already in the comp.lang.python newsgroup and here on the
> tutor mailing list. And that's why I was thinking that if I wrote a function
> like:
> 
> def swap( a, b):
> 	<stuff>
> 
> That the function swap could affect the objects that were passed to a and b.

Ah yes, I forgot the meaning of 'by reference' in other languages.

I meant 1) *Everything* is by reference.

Including the assignments you did in the function, so they didn't have an
effect...

> But, unless a and b are mutable objects, it can't. So saying "everything is by
> reference" is actually confusing and misleading. I like what Tim suggested,
> saying that it is "by object". Anyhow, now I get it. It just took a few times
> to get it through my thick head. That is the usual way with me. My thesis
> advisor has basically said the same thing about me.
> 
> :2 Some objects can be changed, some can't - but no changes are made by
> :  simple assignment to a name.
> :3 Namespaces - everything is just a name for some object, even functions,
> :  modules, the results of import, etc - is this a special case of 1 and 2?
> :4 A variable is global if it's not assigned to here or if there is a 
> :  "global x" statement nearby - otherwise it's local*.
> :5 Things like "def" happen at runtime (almost everything does*)
> :6 Calling a method on some instance passes "self" as 1st argument
> 
> Thanks for this list. I will go over it again and again, until it all makes
> sense to me.

Wait until others give their corrections, I tend to make mistakes...

> :Then you understand all of the key points of the language Python (what did I
> :forget?). I don't think I missed much. This list is enormously smaller than
> :for other languages.
> :
> :Unfortunately the * things become a tiny bit more complex in versions in the
> :near future.
> 
> Er, can't say I'm looking forward to that. I hope it doesn't create
> compatibility issues. My web host is still running 1.5.1, and I daresay a lot
> of people are still using 1.5.2, and not likely to upgrade any time soon.

Not that much is changing. "from __future__ import nested_scopes" acts at
compile time, but you don't have to use it, ignore.

The nested scopes themselves will become mandatory in 2.2 or so. Then the
simple rule becomes something like:
x) If you use a name that isn't assigned to in this scope, it is looked up
  (recursively) in the scope above this one.
  
Which will be *very* cool for the simplified lambdas alone.

> :a, b = b, a
> 
> I already knew about that and posted it in my original question. This was a
> study in passing parameters, so I picked an example I knew how to do without a
> function that was familiar to me. I thought that was a good choice.

I know now, but I had been away for a few days and was reading mail
new-to-old...

-- 
Remco Gerlich