just curious

Collapse
This topic is closed.
X
X
 
  • Time
  • Show
Clear All
new posts
  • Elaine Jackson

    just curious

    I'm new to Python, and I've noticed the following:
    [color=blue][color=green][color=darkred]
    >>> def f(a,b):[/color][/color][/color]
    a+=b[color=blue][color=green][color=darkred]
    >>> def g(a,b):[/color][/color][/color]
    a=a+b[color=blue][color=green][color=darkred]
    >>> p=[1,2,3]
    >>> q=[4,5,6]
    >>> r=[7,8,9]
    >>> s=[10,11,12]
    >>> f(p,q)
    >>> p[/color][/color][/color]
    [1, 2, 3, 4, 5, 6][color=blue][color=green][color=darkred]
    >>> g(r,s)
    >>> r[/color][/color][/color]
    [7, 8, 9]

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

    Peace,
    EJ


  • Erik Max Francis

    #2
    Re: just curious

    Elaine Jackson wrote:
    [color=blue]
    > Any deep reason for this, or "just because"? TIA.[/color]

    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 && max@alcyone.com && 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

    Comment

    • Michael Peuser

      #3
      Re: just curious


      "Elaine Jackson" <elainejackson7 355@home.com> schrieb im Newsbeitrag
      news:rnd6b.9049 24$ro6.18233500 @news2.calgary. shaw.ca...[color=blue]
      > I'm new to Python, and I've noticed the following:[/color]


      (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




      [color=blue]
      >[color=green][color=darkred]
      > >>> def f(a,b):[/color][/color]
      > a+=b[color=green][color=darkred]
      > >>> def g(a,b):[/color][/color]
      > a=a+b[color=green][color=darkred]
      > >>> p=[1,2,3]
      > >>> q=[4,5,6]
      > >>> r=[7,8,9]
      > >>> s=[10,11,12]
      > >>> f(p,q)
      > >>> p[/color][/color]
      > [1, 2, 3, 4, 5, 6][color=green][color=darkred]
      > >>> g(r,s)
      > >>> r[/color][/color]
      > [7, 8, 9]
      >
      > Any deep reason for this, or "just because"? TIA.
      >
      > Peace,
      > EJ
      >
      >[/color]


      Comment

      • News M Claveau /Hamster-P

        #4
        Re: just curious

        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


        Comment

        Working...