[Tutor] Still confused about Python references/objects

Sheila King sheila@thinkspot.net
Sun, 01 Apr 2001 09:08:46 -0700


On Sat, 31 Mar 2001 22:51:39 -0500, "Tim Peters" <tim.one@home.com>  wrote
about RE: [Tutor] Still confused about Python references/objects:

:[Sheila King, on trying to understand Python via reference semantics]
:> 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.)
:
:If it's any consolation, those to whom Python is a first language have no
:trouble at all with it, and probably suffer even greater trying to keep track
:of the new mysteries that pop up when they move to other languages.

Well, that's why I was wondering about whether Python is really a good first
language. It seems to me, it would be much more difficult to get used to
strongly typed languages and references/values/pointers after using Python
first. Dunno.

:The eff-bot (Fredrik Lundh) wrote a very nice and tiny intro, here:
:
:    http://w1.132.telia.com/~u13208596/guides/python-objects.htm
:
:Check it out!  

Been there, done that. I'm one of the types that has to go over an idea many
times until it sinks in.

...<snipped>...

:Yup!  Note too that module-global objects are visible from *all* function and
:method namespaces, so that's another possibility:

I haven't messed with the global module, yet.

...<examples snipped>...

:But heavy use of globals is bad practice, in part precisely *because* any
:call can change global bindings at any time. 

Yes, I teach C++ programming and I tell my students:
global constants = good
global variables = bad

And usually they are not allowed to use global variables in their programs,
precisely because of the reasons you mention below. I think that we did some
stuff with graphics/gui windows once, and IIRC the main window had to be a
global variable in order for the program to work correctly.

: You can see this in the body of
:f:  you can't possibly guess what "print x" is going to do without tracking
:down and studying the source code for g, but you can be certain that "print
:y" will print 42 without looking at g.  This makes the use of y much easier
:to understand in isolation, and the larger your programs get, the more
:valuable it is to limit the amount of code you need to examine.  Local names
:and immutable objects can both help with that.

...<my sample snipped>...

:Little-known feature:  just like you can do
:
:    x, y = two_tuple
:
:and Python will "magically" unpack the two_tuple into x and y, you can also
:do that in formal argument lists.  So
:
:def swap((x, y)):
:    return y, x
:
:also works.

OK, cool. I almost tried that.

:you're-well-on-the-road-to-a-full-recovery<wink>-ly y'rs  - tim

<snort>

--
Sheila King
http://www.thinkspot.net/sheila/
http://www.k12groups.org/