Passing arguments to function - (The fundamentals are confusing me)

Dan dan at cellectivity.com
Tue Aug 9 13:00:52 EDT 2005


> Does that mean Python functions aren't always byref, 
> but are sometimes byval for nonmutables?

Don't think of it as byref or byval (as they are used in Visual Basic).
All parameters are passed the same way: by reference instead of by copy.

It's a little difficult to get your head around, but I promise that once
you understand it it will seem simple and intuitive.

  def reassign(x):
      x = ['foo']

"reassign" has no effect, even on a mutable type like a list. It simply
changes what "x" refers to, which isn't very useful because the name "x"
only exists inside the function.

In Python everything is treated the same way. Even integers are objects:

   >>> n = 1
   >>> n.__hex__()
   '0x1'

What other programming languages do you know? Maybe we can draw a
comparison to something you're familiar with.

-- 
   Presumably, we're all fully qualified computer nerds here,
   so we are allowed to use "access" as a verb. Be advised,
   however, that the practice in common usage drives
   English-language purists to scowling fidgets.
      - from Sybex's "Perl, CGI and JavaScript", p. 256





More information about the Python-list mailing list