behavior varied between empty string '' and empty list []

Discussion in 'Python' started by Tzury Bar Yochay, Mar 24, 2008.

  1. while I can invoke methods of empty string '' right in typing
    (''.join(), etc.) I can't do the same with empty list

    example:

    >>> a = [1,2,3]
    >>> b = [].extend(a)
    >>> b
    >>> b = []
    >>> b.extend(a)
    >>> b

    [1,2,3]

    I would not use b = a since I don't want changes on 'b' to apply on
    'a'

    do you think this should be available on lists to invoke method
    directly?
     
    Tzury Bar Yochay, Mar 24, 2008
    #1
    1. Advertising

  2. En Mon, 24 Mar 2008 15:22:43 -0300, Tzury Bar Yochay
    <> escribió:

    > while I can invoke methods of empty string '' right in typing
    > (''.join(), etc.) I can't do the same with empty list
    >
    > example:
    >
    >>>> a = [1,2,3]
    >>>> b = [].extend(a)
    >>>> b
    >>>> b = []
    >>>> b.extend(a)
    >>>> b

    > [1,2,3]


    extend() -like most mutating methods- does not return the list, it returns
    None.
    Your empty list grow the 3 additional items, but since there were no
    additional references to it, got destroyed.

    > I would not use b = a since I don't want changes on 'b' to apply on
    > 'a'


    Try with b = list(a)

    > do you think this should be available on lists to invoke method
    > directly?


    You already can. Your example is misleading because you used b with two
    meanings.
    (Compare the *usage* of each variable/value, not their names). This is
    equivalent to the second part of your example:

    py> a = [1,2,3]
    py> b = []
    py> b.extend(a)
    py> b
    [1, 2, 3]

    and this is the first part:

    py> a = [1,2,3]
    py> b = []
    py> c = b.extend(a)
    py> c
    py> b
    [1, 2, 3]

    except that in your original example, the empty list had no name so you
    cannot see how it changed.

    --
    Gabriel Genellina
     
    Gabriel Genellina, Mar 24, 2008
    #2
    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. systolic
    Replies:
    3
    Views:
    586
    systolic
    Oct 21, 2004
  2. roopa
    Replies:
    6
    Views:
    781
    Jerry Coffin
    Aug 27, 2004
  3. Pat

    Copying varied types

    Pat, Nov 24, 2009, in forum: C++
    Replies:
    2
    Views:
    275
  4. Laura
    Replies:
    2
    Views:
    328
    Laura
    Feb 18, 2004
  5. rickle
    Replies:
    10
    Views:
    235
Loading...

Share This Page