[Edu-sig] How does Python do Pointers?

kirby urner kirby.urner at gmail.com
Tue May 6 17:10:48 CEST 2008


On Tue, May 6, 2008 at 7:45 AM, David MacQuigg <macquigg at ece.arizona.edu> wrote:
> I agree, there is no reason to dig into call-by-??? terminology with new "untainted" programmers.  The sticky-note analogy is all we need for these students.  The figures in Michael's book are excellent.
>
>  However, having been tainted by C, I am finding the discussion interesting.  I just don't understand why there is so much confusion with these call-by terms.  Does the function get a copy of the object or a reference to the original object?  It's got to be one or the other.

A succinct summary?:

A function gets its own referent for the duration of its scope, but
its a referent to the original object, per names passed in the
"function mouth" (OK to not call it that, but eat( ) has that emoticon
mouth look).

The name is what passes by value, in handing off its value to a local
in-function name, a name behaving sort of like a C pointer, but more
like a generic pointer i.e. like a finger or stick pointing.

IDLE 1.2.1
>>> class Foo:
	pass

>>> o = Foo()
>>> id(o)
15107256

>>> def f(x):  print id(x)

>>> f(o)
15107256

Also (continuing in same namespace)...

>>> def g(x):  x.myname = 'dude'  # add gratuitous attribute to Foo object

>>> g(o)
>>> o.myname  # even though x is out of scope, the original object has changed
'dude'

Thanks to default arguments, we can pass arguments in any order to
some functions:

>>> f()
1 2
>>> f(y=100, x='ardvarrk') # mentioning y first, types irrelevant
ardvarrk 100

I think this is an important feature to dwell on, as many APIs give a
lot of parameters, VPython a great example, but they're all given
defaults, ergo ball(pos=(1,0,0)) and ball(color=(1,0,0)) are both
meaningful.  As the programmer using these APIs, you don't have to
care about order of the names, just the names, and even then you can
skip some.

Kirby

>
>
>  At 10:11 PM 5/5/2008 -0500, Michael H.Goldwasser wrote:
>
>  >  Python's model is quite clean, it just doesn't match up perfectly
>  >  with the standard call-by-??? terminology.
>
>  In call-by-reference evaluation, a function receives an implicit reference to the argument, rather than a copy of its value.
>  -- http://en.wikipedia.org/wiki/Call_by_value
>
>  By this definition, Python's model is call-by-reference, even though the reference (pointer) is not seen by the user.  C's model is call-by-value, even though that value can be an explicitly-evaluated address (pointer).
>
>
>
>  -- Dave
>
>
>  _______________________________________________
>  Edu-sig mailing list
>  Edu-sig at python.org
>  http://mail.python.org/mailman/listinfo/edu-sig
>


More information about the Edu-sig mailing list