scared about refrences...

Inyeol Lee inyeol.lee at siliconimage.com
Wed Nov 1 16:46:41 EST 2006


On Wed, Nov 01, 2006 at 12:20:36PM -0800, SpreadTooThin wrote:
> 
> Bruno Desthuilliers wrote:
> > Nick Vatamaniuc a écrit :
> > (snip)
> > > In  Python all the primitives are copied and all other entities are
> > > references.
> >
> > Plain wrong. There's no "primitives" (ie : primitive data types) in
> > Python, only objects. And they all get passed the same way.
> 
> so..
> def fn(x):
>    x = x + 1
>    print x
> 
> a = 2
> fn(a)
> fn(2)
> 
> Wouldn't you say that this is being passed by value rather than by
> refrence?
> 

What you're confused with is assignment. Check this example;

    >>> def f(x):
    ...     print id(x)
    ...     x = x + 1
    ...     print id(x)
    ... 
    >>> f(1234)
    1617596
    1617608
    >>> a = 5678
    >>> id(a)
    1617596
    >>> f(a)
    1617596
    1617620
    >>>

You can assume id() as a memory address. Is it call-by-value?

-- Inyeol Lee



More information about the Python-list mailing list