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

Gregory Piñero gregpinero at gmail.com
Tue Aug 9 12:32:15 EDT 2005


Ahh, so it's a mutable thing.  That makes sense that I can't change a
mutable object and thus can't affect it outside of the function.  Does
that mean Python functions aren't always byref, but are sometimes
byval for nonmutables?

-Greg


On 8/9/05, Dennis Lee Bieber <wlfraed at ix.netcom.com> wrote:
> On Tue, 9 Aug 2005 10:53:15 -0400, Gregory Piñero <gregpinero at gmail.com >
> declaimed the following in comp.lang.python:
> 
>         <rhetorical> Is this the third time this week that this has come
> up?
> 
> > Hey guys, would someone mind giving me a quick rundown of how
> > references work in Python when passing arguments into functions?  The
> > code below should highlight my specific confusion:
> >
>         They work just like they do everywhere else...
> 
>         Read the manuals on mutable and immutable objects. (I'd suggest
> language reference chapter 3 and 4, library reference chapter 2.3.6,
> 2.3.7)
> 
>         "names" in Python are movable labels attached to objects; they
> are not fixed locations in memory to which object are copied; hence they
> do not behave like variables in traditional languages.
> 
> >
> > bool1=True
> 
>         immutable object -- "bool1" is a label attached to a fixed
> object
> 
> > lst1=[1,2,3]
> >
>         mutable object  -- "lst1" is a label attached to a box
> containing objects
> 
> > def func1(arg1): arg1.append(4)
> >
> 
>         "arg1" is a label attached to whatever object was passed in...
>         .append is an operation that changes what is /inside/ that
> object
> 
> > def func2(arg1): arg1=False
> >
>         "arg1" is a label attached to whatever was passed in...
>         Assignment (especially of an immutable object) takes that label
> OFF of the object that was passed in, and moves it the object of the
> assignment. It does not move the label that is outside the call.
> 
> > >>func1(lst1)
> > >>lst1
> > [1,2,3,4]
> >
>         "lst1" is the label of the box; inside of func1, that box has
> two labels: "lst1" and "arg1". You used the "arg1" label to locate the
> box, and then you changed what was inside the box. Outside the function,
> you used the "lst1" label to find the /same/ box and report what was
> inside it.
> 
> > >>func2(bool1)
> > >>bool1
> > True
> >
>         "bool1" is the label of a non-box -- a "true". Inside the
> function "true" has initially two labels: "bool1" and "arg1". You then
> moved the "arg1" label from "true" to a different object "false".
> "bool1" does not move, and still references the "true".
> 
> 
> --
>  > ============================================================== <
>  >   wlfraed at ix.netcom.com  | Wulfraed  Dennis Lee Bieber  KD6MOG <
>  >      wulfraed at dm.net     |       Bestiaria Support Staff       <
>  > ============================================================== <
>  >           Home Page: <http://www.dm.net/~wulfraed/ >            <
>  >        Overflow Page: <http://wlfraed.home.netcom.com/ >        <
> --
> http://mail.python.org/mailman/listinfo/python-list 
> 


-- 
Gregory Piñero
Chief Innovation Officer
Blended Technologies
(www.blendedtechnologies.com)



More information about the Python-list mailing list