just curious

Discussion in 'Python' started by Elaine Jackson, Sep 6, 2003.

  1. I'm new to Python, and I've noticed the following:

    >>> def f(a,b):

    a+=b
    >>> def g(a,b):

    a=a+b
    >>> p=[1,2,3]
    >>> q=[4,5,6]
    >>> r=[7,8,9]
    >>> s=[10,11,12]
    >>> f(p,q)
    >>> p

    [1, 2, 3, 4, 5, 6]
    >>> g(r,s)
    >>> r

    [7, 8, 9]

    Any deep reason for this, or "just because"? TIA.

    Peace,
    EJ
     
    Elaine Jackson, Sep 6, 2003
    #1
    1. Advertising

  2. Elaine Jackson wrote:

    > Any deep reason for this, or "just because"? TIA.


    It's because the augmented assignment operators, like +=, are supposed
    to efficiently mutate objects when they have the opportunity (at least
    for the builtin classes). So a = a + b always creates a new object, but
    a += b might just mutate a preexisting object if that option is
    available.

    --
    Erik Max Francis && && http://www.alcyone.com/max/
    __ San Jose, CA, USA && 37 20 N 121 53 W && &tSftDotIotE
    / \ The great floodgates of the wonder-world swung open.
    \__/ Herman Melville
     
    Erik Max Francis, Sep 6, 2003
    #2
    1. Advertising

  3. "Elaine Jackson" <> schrieb im Newsbeitrag
    news:rnd6b.904924$...
    > I'm new to Python, and I've noticed the following:



    (1) Python has a quite strict "call by value" similar to C. This means - as
    in C) you can use formal parameters as if they were local variables. (And
    there are some useful tricks with that..)
    So A = A + B will have no outside impact.

    (2) A += B for lists is a shortcut for
    A.extend(B)
    which means that it changes something A is bound to (or "points to" as you
    would say in C). This is somewhat awkward because it sometimes works counter
    intuitive as in your case. Just keep in mind: A+=B is *not* A=A+B but
    behaves as if in most cases.... ;-)

    Kindly
    Michael P





    >
    > >>> def f(a,b):

    > a+=b
    > >>> def g(a,b):

    > a=a+b
    > >>> p=[1,2,3]
    > >>> q=[4,5,6]
    > >>> r=[7,8,9]
    > >>> s=[10,11,12]
    > >>> f(p,q)
    > >>> p

    > [1, 2, 3, 4, 5, 6]
    > >>> g(r,s)
    > >>> r

    > [7, 8, 9]
    >
    > Any deep reason for this, or "just because"? TIA.
    >
    > Peace,
    > EJ
    >
    >
     
    Michael Peuser, Sep 6, 2003
    #3
  4. Hi !

    In
    def g(a,b):
    a=a+b

    a new object "a" is create ; but it's a local object, who are not the "a"
    global.
    In python, variables are "pointer to object".

    If you try :
    def g(a,b):
    global a
    a=a+b
    You obtain : "name 'a' is global and local"

    @-salutations
    --
    Michel Claveau
     
    News M Claveau /Hamster-P, Sep 6, 2003
    #4
    1. Advertising

Want to reply to this thread or ask your own question?

It takes just 2 minutes to sign up (and it's free!). Just click the sign up button to choose a username and then you can ask your own questions on the forum.
Similar Threads
  1. Replies:
    2
    Views:
    378
    Robert Klemme
    Jan 6, 2006
  2. Ken Parkes

    Just curious

    Ken Parkes, Sep 20, 2004, in forum: Python
    Replies:
    4
    Views:
    328
    Ken Parkes
    Sep 22, 2004
  3. lallous
    Replies:
    5
    Views:
    355
  4. =?Utf-8?B?Qi4gQ2hlcm5pY2s=?=

    Gridview question - Just curious

    =?Utf-8?B?Qi4gQ2hlcm5pY2s=?=, Nov 12, 2007, in forum: ASP .Net
    Replies:
    1
    Views:
    308
    Eliyahu Goldin
    Nov 12, 2007
  5. Virchanza
    Replies:
    6
    Views:
    253
    Noah Roberts
    May 16, 2011
Loading...

Share This Page