python needs leaning stuff from other language

Discussion in 'Python' started by Armin, Apr 2, 2009.

  1. Armin

    Armin Guest

    On Thursday 02 April 2009 22:40:08 Zac Burns wrote:
    > Is it really worth it to not implement list.clear and answer this
    > question over and over again?
    >
    > I see no reason that a list shouldn't have a .clear method.
    >
    > --
    > Zachary Burns
    > (407)590-4814
    > Aim - Zac256FL
    > Production Engineer (Digital Overlord)
    > Zindagi Games
    >
    > On Thu, Apr 2, 2009 at 5:32 PM, Esmail <> wrote:
    > > Emile van Sebille wrote:
    > >> Esmail wrote:
    > >>> Diez B. Roggisch wrote:
    > >>>> schrieb:
    > >>>>> python's list needs a thing list.clear() like c# arraylist
    > >>>>> and
    > >>>>
    > >>>> some_list[:] = []
    > >>>
    > >>> I agree that this is nice and clear, but as a relative newbie
    > >>> wouldn't
    > >>>
    > >>> some_list = []
    > >>
    > >> This is different -- it creates a new list. Consider:
    > >>
    > >> >>> some_list = [1,2,3]
    > >> >>> d = some_list
    > >> >>> d[1]
    > >> 2
    > >> >>> some_list[:] = ['a','b','c']
    > >> >>> d[1]
    > >> 'b'
    > >> >>> some_list = [1,2,3]
    > >> >>> d[1]
    > >> 'b'
    > >>
    > >> the [:] form allows references into the list to remain valid while the
    > >> direct assignment dopes not.

    > >
    > > Ah .. thanks for clarifying this .. makes sense.
    > >
    > > Also, thank you Luis for your post.
    > >
    > > Esmail
    > >
    > > --
    > > http://mail.python.org/mailman/listinfo/python-list

    >
    > --
    > http://mail.python.org/mailman/listinfo/python-list


    A .clear() method wouldn't be beneficial:

    del mylist[:]

    --
    Armin Moradi
     
    Armin, Apr 2, 2009
    #1
    1. Advertising

  2. Armin

    Guest

    python's list needs a thing list.clear() like c# arraylist
    and
    python needs a writeline() method
     
    , Apr 2, 2009
    #2
    1. Advertising

  3. schrieb:
    > python's list needs a thing list.clear() like c# arraylist
    > and


    some_list[:] = []

    > python needs a writeline() method


    print()

    Diez
     
    Diez B. Roggisch, Apr 2, 2009
    #3
  4. Armin

    Esmail Guest

    Diez B. Roggisch wrote:
    > schrieb:
    >> python's list needs a thing list.clear() like c# arraylist
    >> and

    >
    > some_list[:] = []


    I agree that this is nice and clear, but as a relative newbie
    wouldn't

    some_list = []

    be also acceptable (and pythonic?)?
     
    Esmail, Apr 2, 2009
    #4
  5. Armin

    Esmail Guest

    Diez B. Roggisch wrote:
    > schrieb:
    >> python's list needs a thing list.clear() like c# arraylist
    >> and

    >
    > some_list[:] = []


    I agree that this is nice and clear, but as a relative newbie
    wouldn't

    some_list = []

    be also acceptable (and pythonic?)?
     
    Esmail, Apr 2, 2009
    #5
  6. Quoting :

    > python's list needs a thing list.clear() like c# arraylist


    It has:

    >>> l[:] = []


    > python needs a writeline() method


    Now, that could be useful, a writeline method that knew the EOL convention for
    the OS and were not as deceiving as the current .writelines().

    BTW, check out the print statement from python2, and the print function in python3:

    >>> print >>fileobj, "this will print with an eol" # python2
    >>> print("this will print with an eol", file=fileobj)


    --
    Luis Zarrabeitia
    Facultad de Matemática y Computación, UH
    http://profesores.matcom.uh.cu/~kyrie

    --
    Participe en Universidad 2010, del 8 al 12 de febrero de 2010
    La Habana, Cuba
    http://www.universidad2010.cu
     
    Luis Alberto Zarrabeitia Gomez, Apr 2, 2009
    #6
  7. Quoting Esmail <>:

    > Diez B. Roggisch wrote:
    > >
    > > some_list[:] = []

    >
    > I agree that this is nice and clear,


    Not very intuitive, though, until you learn the syntax.
    (but, slice accessing and slice assignment are among the first few things one
    learns about python anyway, and once you learn it, it seems more than natural)

    > but as a relative newbie
    > wouldn't
    >
    > some_list = []
    >
    > be also acceptable (and pythonic?)?


    Pythonic, yes.
    Acceptable, it may not be (depending on the problem). And I would adventure that
    if someone needs to clear a list instead of creating an empty one, its likely
    that someone is facing one of those problems (i.e: clearing a list that you got
    as a function argument, or playing with os.walk).

    --
    Luis Zarrabeitia
    Facultad de Matemática y Computación, UH
    http://profesores.matcom.uh.cu/~kyrie




    --
    Participe en Universidad 2010, del 8 al 12 de febrero de 2010
    La Habana, Cuba
    http://www.universidad2010.cu
     
    Luis Alberto Zarrabeitia Gomez, Apr 3, 2009
    #7
  8. Esmail wrote:
    > Diez B. Roggisch wrote:
    >> schrieb:
    >>> python's list needs a thing list.clear() like c# arraylist
    >>> and

    >>
    >> some_list[:] = []

    >
    > I agree that this is nice and clear, but as a relative newbie
    > wouldn't
    >
    > some_list = []


    This is different -- it creates a new list. Consider:

    >>> some_list = [1,2,3]
    >>> d = some_list
    >>> d[1]

    2
    >>> some_list[:] = ['a','b','c']
    >>> d[1]

    'b'
    >>> some_list = [1,2,3]
    >>> d[1]

    'b'

    the [:] form allows references into the list to remain valid while the
    direct assignment dopes not.

    Emile



    >
    > be also acceptable (and pythonic?)?
    >
    > --
    > http://mail.python.org/mailman/listinfo/python-list
    >
     
    Emile van Sebille, Apr 3, 2009
    #8
  9. Armin

    Gary Herron Guest

    wrote:
    > python's list needs a thing list.clear() like c# arraylist
    > and
    > python needs a writeline() method
    >
    > --
    > http://mail.python.org/mailman/listinfo/python-list
    >


    (While you are correct that Python needs these things, a better
    attitude, as a newbie, would be to *ask* how Python supplies these
    features you want, because in fact, they have been included in the
    language for a very long time.)

    Both
    some_list[:] = []
    and
    del some_list[:]
    will clear the contents of a list, but will leave the identity of the
    list unchanged.

    That is
    A = [1,2,3]
    B = A
    del A[:]
    will leave both A and B referencing the same empty list.

    Alternatively
    A = [1,2,3]
    B = A
    A = []
    will leave A referencing a new empty list,
    but B will reference the original [1,2,3] list.



    As for a writeline(), and guessing what it is you want, I'd say look at the
    print>>outfile, ...
    form in Python 2.5. In Python 3.X, the
    print(...)
    function will get you the functionality (I think) you want.

    Gary Herron
     
    Gary Herron, Apr 3, 2009
    #9
  10. Armin

    Esmail Guest

    Emile van Sebille wrote:
    > Esmail wrote:
    >> Diez B. Roggisch wrote:
    >>> schrieb:
    >>>> python's list needs a thing list.clear() like c# arraylist
    >>>> and
    >>>
    >>> some_list[:] = []

    >>
    >> I agree that this is nice and clear, but as a relative newbie
    >> wouldn't
    >>
    >> some_list = []

    >
    > This is different -- it creates a new list. Consider:
    >
    > >>> some_list = [1,2,3]
    > >>> d = some_list
    > >>> d[1]

    > 2
    > >>> some_list[:] = ['a','b','c']
    > >>> d[1]

    > 'b'
    > >>> some_list = [1,2,3]
    > >>> d[1]

    > 'b'
    >
    > the [:] form allows references into the list to remain valid while the
    > direct assignment dopes not.


    Ah .. thanks for clarifying this .. makes sense.

    Also, thank you Luis for your post.

    Esmail
     
    Esmail, Apr 3, 2009
    #10
  11. Armin

    Zac Burns Guest

    Is it really worth it to not implement list.clear and answer this
    question over and over again?

    I see no reason that a list shouldn't have a .clear method.

    --
    Zachary Burns
    (407)590-4814
    Aim - Zac256FL
    Production Engineer (Digital Overlord)
    Zindagi Games



    On Thu, Apr 2, 2009 at 5:32 PM, Esmail <> wrote:
    > Emile van Sebille wrote:
    >>
    >> Esmail wrote:
    >>>
    >>> Diez B. Roggisch wrote:
    >>>>
    >>>> schrieb:
    >>>>>
    >>>>> python's list needs a thing  list.clear()  like c# arraylist
    >>>>> and
    >>>>
    >>>> some_list[:] = []
    >>>
    >>> I agree that this is nice and clear, but as a relative newbie
    >>> wouldn't
    >>>
    >>> some_list = []

    >>
    >> This is different -- it creates a new list.  Consider:
    >>
    >>  >>> some_list = [1,2,3]
    >>  >>> d = some_list
    >>  >>> d[1]
    >> 2
    >>  >>> some_list[:] = ['a','b','c']
    >>  >>> d[1]
    >> 'b'
    >>  >>> some_list = [1,2,3]
    >>  >>> d[1]
    >> 'b'
    >>
    >> the [:] form allows references into the list to remain valid while the
    >> direct assignment dopes not.

    >
    > Ah .. thanks for clarifying this .. makes sense.
    >
    > Also, thank you Luis for your post.
    >
    > Esmail
    >
    > --
    > http://mail.python.org/mailman/listinfo/python-list
    >
     
    Zac Burns, Apr 3, 2009
    #11
  12. Armin

    Terry Reedy Guest

    Diez B. Roggisch wrote:
    > schrieb:
    >> python's list needs a thing list.clear() like c# arraylist
    >> and

    >
    > some_list[:] = []


    Or
    del some_list[:]
     
    Terry Reedy, Apr 3, 2009
    #12
  13. Armin

    MRAB Guest

    Zac Burns wrote:
    > Is it really worth it to not implement list.clear and answer this
    > question over and over again?
    >
    > I see no reason that a list shouldn't have a .clear method.
    >

    Does dict have a .clear method? Yes.

    Does set have a .clear method? Yes.

    Does list have a .clear method? No.

    Of course, with a list you can do del my_list[:]; there's no simple
    equivalent for dict or set.

    But, overall, I'm +1.
     
    MRAB, Apr 3, 2009
    #13
  14. On Thu, 02 Apr 2009 18:40:08 -0700, Zac Burns wrote:

    > Is it really worth it to not implement list.clear and answer this
    > question over and over again?
    >
    > I see no reason that a list shouldn't have a .clear method.



    The usual answer to that is that there's already two ways of clearing a
    list:

    del alist[:]
    alist[:] = []

    and we don't need a third way. Dicts and sets need a clear() method,
    because there's no equivalent to slicing.

    I still think that alist.clear() would be a fine addition that matches my
    intuition and aesthetic sense. Alas, I'm apparently not Dutch.


    BTW Zac, on this list we don't appreciate top-posting, and we encourage
    people to trim the quoted replies.



    --
    Steven
     
    Steven D'Aprano, Apr 3, 2009
    #14
  15. Armin

    Zamnedix Guest

    On Apr 2, 3:25 pm, wrote:
    > python's list needs a thing list.clear() like c# arraylist
    > and
    > python needs a writeline() method


    Please don't post things like list before you do any research.
    You don't know what you are talking about.
     
    Zamnedix, Apr 3, 2009
    #15
  16. Armin

    Mel Guest

    Ben Finney wrote:

    > I think it would also be better to have One (and prefereably Only One)
    > Obvious Way To Do It. That obvious way, for those who work with
    > Python's ‘set’ and ‘dict’, is a ‘clear’ method. It seems best to have
    > ‘list’ conform with this also.


    Does that mean a one-off special case rule to forbid slices having a
    default?

    Mel.
     
    Mel, Apr 3, 2009
    #16
  17. On Fri, 03 Apr 2009 08:23:22 -0700, Zamnedix wrote:

    > On Apr 2, 3:25 pm, wrote:
    >> python's list needs a thing list.clear() like c# arraylist and
    >> python needs a writeline() method

    >
    > Please don't post things like list before you do any research. You don't
    > know what you are talking about.


    The original poster may or may not know what he is talking about, but
    adding a clear() method to lists seems to be very much in demand. I'd
    vote Yes for one.

    Besides, this news group is for people to ask questions about Python,
    even stupid questions. It's not just for experts only.



    --
    Steven
     
    Steven D'Aprano, Apr 3, 2009
    #17
  18. On Fri, 03 Apr 2009 11:41:10 -0400, Mel wrote:

    > Ben Finney wrote:
    >
    >> I think it would also be better to have One (and prefereably Only One)
    >> Obvious Way To Do It. That obvious way, for those who work with
    >> Python's ‘set’ and ‘dict’, is a ‘clear’ method. It seems best to have
    >> ‘list’ conform with this also.

    >
    > Does that mean a one-off special case rule to forbid slices having a
    > default?


    Why would it do that?


    --
    Steven
     
    Steven D'Aprano, Apr 3, 2009
    #18
  19. Armin

    Mel Guest

    Steven D'Aprano wrote:

    > On Fri, 03 Apr 2009 11:41:10 -0400, Mel wrote:
    >
    >> Ben Finney wrote:
    >>
    >>> I think it would also be better to have One (and prefereably Only One)
    >>> Obvious Way To Do It. That obvious way, for those who work with
    >>> Python's ‘set’ and ‘dict’, is a ‘clear’ method. It seems best to have
    >>> ‘list’ conform with this also.

    >>
    >> Does that mean a one-off special case rule to forbid slices having a
    >> default?

    >
    > Why would it do that?


    Well, if list.clear were truly and strictly to be the only way to clear the
    contents of a list, then assigning nothing via the default slice would have
    to be ruled out. `somelist[:] = []` is just a special case of assignment to
    a slice generally.

    Mel.
     
    Mel, Apr 3, 2009
    #19
  20. Armin

    Tim Wintle Guest

    On Fri, 2009-04-03 at 13:12 -0400, Mel wrote:
    > >>> I think it would also be better to have One (and prefereably Only One)
    > >>> Obvious Way To Do It. That obvious way, for those who work with
    > >>> Python's ‘set’ and ‘dict’, is a ‘clear’ method. It seems best to have
    > >>> ‘list’ conform with this also.
    > >>
    > >> Does that mean a one-off special case rule to forbid slices having a
    > >> default?

    > >
    > > Why would it do that?

    >
    > Well, if list.clear were truly and strictly to be the only way to clear the
    > contents of a list, then assigning nothing via the default slice would have
    > to be ruled out. `somelist[:] = []` is just a special case of assignment to
    > a slice generally.


    agreed. If .clear was to be added then really assignments to slices
    should be entirely removed.

    Tim W
     
    Tim Wintle, Apr 3, 2009
    #20
    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. =?Utf-8?B?Tkg=?=

    Design and other stuff

    =?Utf-8?B?Tkg=?=, Oct 6, 2005, in forum: ASP .Net
    Replies:
    3
    Views:
    385
    Eliyahu Goldin
    Oct 6, 2005
  2. Darren Kopp
    Replies:
    11
    Views:
    1,229
    =?Utf-8?B?TWF0dCBUb3dlcnM=?=
    Feb 24, 2006
  3. Peter Charles
    Replies:
    12
    Views:
    1,145
    Lauri Raittila
    Jan 24, 2004
  4. jean-marc

    Thanks for PIL (and other stuff)

    jean-marc, Jun 24, 2005, in forum: Python
    Replies:
    3
    Views:
    291
    Rocco Moretti
    Jun 24, 2005
  5. Antoninus Twink

    Left leaning red black trees

    Antoninus Twink, Dec 2, 2009, in forum: C Programming
    Replies:
    10
    Views:
    2,420
Loading...

Share This Page