[Edu-sig] How does Python do Pointers?

kirby urner kirby.urner at gmail.com
Wed May 7 18:49:39 CEST 2008


>  >  I like the point made earlier that parameter passing at the function
>  >  gateway is all about assignment, and assignment is all about cloning a
>  >  pointer or reference, for use within the local scope, and these names
>  >  are by definition something tiny, whereas the objects themselves --
>  >  who knows how big these might be.
>  >
>  >  Kirby
>

Per my blog entry on this topic (mostly just pointing back here

http://mybizmo.blogspot.com/2008/05/memory-managment.html

), you might explicitly do the assignment x = y right in the
function header e.g. def g (x = y): is legal provide y has meaning
in the global scope by the time we reach this definition.

So to prove arguments "pass by assignment" you could go:

>>> class MegaLith:
	pass

>>> y = MegaLith()    # y gets to control one

>>> def f(x = y):
	x.color = "grey"  #  x does too, interimly...

Note you'd get an error if y weren't already defined.  The
more normal think is to have your right side defaults
be hard-coded values, not variables (names).
	
>>> f( )              # don't forget to actually do the work
>>> y.color
'grey'
>>> # tada!
>>>

The picture here would be x and y both tied to the same
MegaLith object, but x only while we're in the scope of
function f.

Kirby


More information about the Edu-sig mailing list