
Hi everyone, On the general Python list, a suggestion was made to add a clear() method to list, as the "obvious" way to do del some_list[:] or some_list[:] = [] since the clear() method is currently the obvious way to remove all elements from dict and set objects. I believe that this would be a lot more intuitive to beginners learning the language, making Python more uniform. André

On 2009-04-03, Andre Roberge wrote:
Hi, I have a use case for list.clear() (might be a bit obscure though). If you have a class that includes a list as an attribute (e.g., a list "subclass" that uses aggregation rather than inheritance), you might want to delegate many list methods to the list attribute and only implement those you want to treat specially. I show an example of this in "Programming in Python 3" (pages 367/8) where I have a @delegate decorator that accepts an attribute name and a tuple of methods to delegate to, e.g.: @delegate("__list", ("pop", "__delitem__", "__getitem_", ...)) class MyList: ... def clear(self): self.__list = [] But because there is no list.clear(), the clear() method must be implemented rather than delegated even though it doesn't do anything special. +1 -- Mark Summerfield, Qtrac Ltd, www.qtrac.eu C++, Python, Qt, PyQt - training and consultancy "Programming in Python 3" - ISBN 0137129297

About your example, what is the advantage over inheriting from list? I did this myself to build a kind of treed list class that supports nested lists: Class TreeList(list): # uses most list methods def __iter__: # does a recursive descent through nested lists. I only had to implement methods that I wanted to add some extra sauce to. On Fri, Apr 3, 2009 at 7:15 AM, Mark Summerfield <list@qtrac.plus.com> wrote:
-- Gerald Britton

On Fri, Apr 3, 2009 at 1:33 PM, Gerald Britton <gerald.britton@gmail.com> wrote:
About your example, what is the advantage over inheriting from list?
The fact that you can pick and choose which methods to provide, while with inheritance you get all of them whether you want them or not. For example one may want to create a FixedSizeList that provides only the read-only methods plus __setitem__, but not any method that changes the size of the list. Although you could emulate that with inheritance (by making all forbidden methods raise AttributeError), it's less clear than delegation and it breaks introspection. George

On 2009-04-03, Gerald Britton wrote:
Yes, but sometimes you need something that offers _less_ functionality than a list (e.g., you could create a stack or queue by aggregating a list), in which case it is easier to aggregate and just add or delegate the methods you want without having to worry about "unimplementing" those that you don't want---although unimplementing is possible of course.
On Fri, Apr 3, 2009 at 7:15 AM, Mark Summerfield <list@qtrac.plus.com>
wrote:
-- Mark Summerfield, Qtrac Ltd, www.qtrac.eu C++, Python, Qt, PyQt - training and consultancy "C++ GUI Programming with Qt 4" - ISBN 0132354160

On 3 Apr, 12:53, Andre Roberge <andre.robe...@gmail.com> wrote:
I always wondered why there wasn't such a thing from the beginning. + 1 from me. --- Giampaolo http://code.google.com/p/pyftpdlib

+1 from me The current methods are all slightly ugly. Michael 2009/4/3 Giampaolo Rodola' <gnewsg@gmail.com>

i instinctively want to do add instead of append. some of the other functions i would want to use are Contains, maybe IndexOf. is there any kind of plugin for a query language like LINQ for python? thatd be dope -Adele (sent from my gphone!) On Apr 3, 2009 11:55 AM, "George Sakkis" <george.sakkis@gmail.com> wrote: On Fri, Apr 3, 2009 at 6:53 AM, Andre Roberge <andre.roberge@gmail.com> wrote: > Hi everyone, > > O... It's obviously more explicit, but at the same it's a rather infrequent operation and the current ways are not particularly ugly. +0, no strong feelings either way. George _______________________________________________ Python-ideas mailing list Python-ideas@python.org ht...

2009/4/3 Paige Thompson <erratic@devel.ws>
i instinctively want to do add instead of append. some of the other functions i would want to use are Contains, maybe IndexOf.
Sound like you want to be using a different language! Lists support all those operations they are just spelled differently...
is there any kind of plugin for a query language like LINQ for python? thatd be dope
We have list comprehensions which are LINQ over objects on steroids, however Python has nothing like LINQ to other data providers built into the language. There is a third party extension / framework / tool called DejaVu though, which I have heard good things about. Michael

because im just a troll -Adele (sent from my gphone!) On Apr 3, 2009 1:24 PM, "George Sakkis" <george.sakkis@gmail.com> wrote: On Fri, Apr 3, 2009 at 3:09 PM, Paige Thompson <erratic@devel.ws> wrote: > i instinctively want to ... If you're so happy with C#, why bother with Python in the first place ?
Python-ideas mailing list Python-ideas@python.org http://mail.python.org/mailman/listinfo/python-ideas

if any of you are not C# coders, generics and IEnumerables/IQueryables are whats god to me. We have lists which is a generic, though the genrics part is kind of type-safe specific. Example: List<string> mylist = new List<string>(); // cant remember if thats the exact syntax but thats essentially *it* you could also do List<object> hodgepodge .... could be strings ints guids etc. anyway i think c# lists and dictionaries (also generic) are god. Dictionary<guid, string> at any rate, typesafe or not the list and dictionary objects have a lot of neat stuff inside of them, some of which are LINQ (essentially an inline query language) specific. maybe some of the devs would like to steal some of the brilliance out of there-- their claim to fame is patterns and practices. Having worked with these and not to put python down in anyway at all i think their lists and dictionaries (generics) are wonderful to work with. I hear java is similar. -Adele (sent from my gphone!) On Apr 3, 2009 11:55 AM, "George Sakkis" <george.sakkis@gmail.com> wrote: On Fri, Apr 3, 2009 at 6:53 AM, Andre Roberge <andre.roberge@gmail.com> wrote: > Hi everyone, > > O... It's obviously more explicit, but at the same it's a rather infrequent operation and the current ways are not particularly ugly. +0, no strong feelings either way. George _______________________________________________ Python-ideas mailing list Python-ideas@python.org ht...

George Sakkis <george.sakkis@gmail.com> writes:
More than explicit, it would make ‘clear’ the One Obvious Way To Do It for base collection types. +1 from me. -- \ “It's a good thing we have gravity or else when birds died | `\ they'd just stay right up there. Hunters would be all | _o__) confused.” —Steven Wright | Ben Finney

Andre Roberge wrote:
Seeing mostly positive responses so far ... Is it worth writing a pre-PEP to formalize this suggestion?
The feature doesn't require a formal PEP. The implementation requires 10 lines of simple C code. For a full patch you have to provide a simple _abscoll.MutableSequence.clear() method (just pop() until an IndexError is raised), some documentation updates and a bunch of unit tests. Christian Index: Objects/listobject.c =================================================================== --- Objects/listobject.c (revision 71106) +++ Objects/listobject.c (working copy) @@ -785,6 +785,14 @@ } static PyObject * +listclear(PyListObject *self, PyObject *unused) +{ + if (list_resize(self, 0) == 0) + Py_RETURN_NONE; + return NULL; +} + +static PyObject * listextend(PyListObject *self, PyObject *b) { PyObject *it; /* iter(v) */ @@ -2458,6 +2466,8 @@ "L.__sizeof__() -- size of L in memory, in bytes"); PyDoc_STRVAR(append_doc, "L.append(object) -- append object to end"); +PyDoc_STRVAR(clear_doc, +"L.clear() -- clear the list"); PyDoc_STRVAR(extend_doc, "L.extend(iterable) -- extend list by appending elements from the iterable"); PyDoc_STRVAR(insert_doc, @@ -2486,6 +2496,7 @@ {"__reversed__",(PyCFunction)list_reversed, METH_NOARGS, reversed_doc}, {"__sizeof__", (PyCFunction)list_sizeof, METH_NOARGS, sizeof_doc}, {"append", (PyCFunction)listappend, METH_O, append_doc}, + {"clear", (PyCFunction)listclear, METH_NOARGS, clear_doc}, {"insert", (PyCFunction)listinsert, METH_VARARGS, insert_doc}, {"extend", (PyCFunction)listextend, METH_O, extend_doc}, {"pop", (PyCFunction)listpop, METH_VARARGS, pop_doc},

Andre Roberge <andre.roberge@gmail.com> writes:
I wouldn't think it worth a PEP. Simpler to produce a patch implementing and documenting the change, and see what reception it gets on python-dev. -- \ “I got an answering machine for my phone. Now when someone | `\ calls me up and I'm not home, they get a recording of a busy | _o__) signal.” —Steven Wright | Ben Finney

On 2009-04-03, Andre Roberge wrote:
Hi, I have a use case for list.clear() (might be a bit obscure though). If you have a class that includes a list as an attribute (e.g., a list "subclass" that uses aggregation rather than inheritance), you might want to delegate many list methods to the list attribute and only implement those you want to treat specially. I show an example of this in "Programming in Python 3" (pages 367/8) where I have a @delegate decorator that accepts an attribute name and a tuple of methods to delegate to, e.g.: @delegate("__list", ("pop", "__delitem__", "__getitem_", ...)) class MyList: ... def clear(self): self.__list = [] But because there is no list.clear(), the clear() method must be implemented rather than delegated even though it doesn't do anything special. +1 -- Mark Summerfield, Qtrac Ltd, www.qtrac.eu C++, Python, Qt, PyQt - training and consultancy "Programming in Python 3" - ISBN 0137129297

About your example, what is the advantage over inheriting from list? I did this myself to build a kind of treed list class that supports nested lists: Class TreeList(list): # uses most list methods def __iter__: # does a recursive descent through nested lists. I only had to implement methods that I wanted to add some extra sauce to. On Fri, Apr 3, 2009 at 7:15 AM, Mark Summerfield <list@qtrac.plus.com> wrote:
-- Gerald Britton

On Fri, Apr 3, 2009 at 1:33 PM, Gerald Britton <gerald.britton@gmail.com> wrote:
About your example, what is the advantage over inheriting from list?
The fact that you can pick and choose which methods to provide, while with inheritance you get all of them whether you want them or not. For example one may want to create a FixedSizeList that provides only the read-only methods plus __setitem__, but not any method that changes the size of the list. Although you could emulate that with inheritance (by making all forbidden methods raise AttributeError), it's less clear than delegation and it breaks introspection. George

On 2009-04-03, Gerald Britton wrote:
Yes, but sometimes you need something that offers _less_ functionality than a list (e.g., you could create a stack or queue by aggregating a list), in which case it is easier to aggregate and just add or delegate the methods you want without having to worry about "unimplementing" those that you don't want---although unimplementing is possible of course.
On Fri, Apr 3, 2009 at 7:15 AM, Mark Summerfield <list@qtrac.plus.com>
wrote:
-- Mark Summerfield, Qtrac Ltd, www.qtrac.eu C++, Python, Qt, PyQt - training and consultancy "C++ GUI Programming with Qt 4" - ISBN 0132354160

On 3 Apr, 12:53, Andre Roberge <andre.robe...@gmail.com> wrote:
I always wondered why there wasn't such a thing from the beginning. + 1 from me. --- Giampaolo http://code.google.com/p/pyftpdlib

+1 from me The current methods are all slightly ugly. Michael 2009/4/3 Giampaolo Rodola' <gnewsg@gmail.com>

i instinctively want to do add instead of append. some of the other functions i would want to use are Contains, maybe IndexOf. is there any kind of plugin for a query language like LINQ for python? thatd be dope -Adele (sent from my gphone!) On Apr 3, 2009 11:55 AM, "George Sakkis" <george.sakkis@gmail.com> wrote: On Fri, Apr 3, 2009 at 6:53 AM, Andre Roberge <andre.roberge@gmail.com> wrote: > Hi everyone, > > O... It's obviously more explicit, but at the same it's a rather infrequent operation and the current ways are not particularly ugly. +0, no strong feelings either way. George _______________________________________________ Python-ideas mailing list Python-ideas@python.org ht...

2009/4/3 Paige Thompson <erratic@devel.ws>
i instinctively want to do add instead of append. some of the other functions i would want to use are Contains, maybe IndexOf.
Sound like you want to be using a different language! Lists support all those operations they are just spelled differently...
is there any kind of plugin for a query language like LINQ for python? thatd be dope
We have list comprehensions which are LINQ over objects on steroids, however Python has nothing like LINQ to other data providers built into the language. There is a third party extension / framework / tool called DejaVu though, which I have heard good things about. Michael

because im just a troll -Adele (sent from my gphone!) On Apr 3, 2009 1:24 PM, "George Sakkis" <george.sakkis@gmail.com> wrote: On Fri, Apr 3, 2009 at 3:09 PM, Paige Thompson <erratic@devel.ws> wrote: > i instinctively want to ... If you're so happy with C#, why bother with Python in the first place ?
Python-ideas mailing list Python-ideas@python.org http://mail.python.org/mailman/listinfo/python-ideas

if any of you are not C# coders, generics and IEnumerables/IQueryables are whats god to me. We have lists which is a generic, though the genrics part is kind of type-safe specific. Example: List<string> mylist = new List<string>(); // cant remember if thats the exact syntax but thats essentially *it* you could also do List<object> hodgepodge .... could be strings ints guids etc. anyway i think c# lists and dictionaries (also generic) are god. Dictionary<guid, string> at any rate, typesafe or not the list and dictionary objects have a lot of neat stuff inside of them, some of which are LINQ (essentially an inline query language) specific. maybe some of the devs would like to steal some of the brilliance out of there-- their claim to fame is patterns and practices. Having worked with these and not to put python down in anyway at all i think their lists and dictionaries (generics) are wonderful to work with. I hear java is similar. -Adele (sent from my gphone!) On Apr 3, 2009 11:55 AM, "George Sakkis" <george.sakkis@gmail.com> wrote: On Fri, Apr 3, 2009 at 6:53 AM, Andre Roberge <andre.roberge@gmail.com> wrote: > Hi everyone, > > O... It's obviously more explicit, but at the same it's a rather infrequent operation and the current ways are not particularly ugly. +0, no strong feelings either way. George _______________________________________________ Python-ideas mailing list Python-ideas@python.org ht...

George Sakkis <george.sakkis@gmail.com> writes:
More than explicit, it would make ‘clear’ the One Obvious Way To Do It for base collection types. +1 from me. -- \ “It's a good thing we have gravity or else when birds died | `\ they'd just stay right up there. Hunters would be all | _o__) confused.” —Steven Wright | Ben Finney

Andre Roberge wrote:
Seeing mostly positive responses so far ... Is it worth writing a pre-PEP to formalize this suggestion?
The feature doesn't require a formal PEP. The implementation requires 10 lines of simple C code. For a full patch you have to provide a simple _abscoll.MutableSequence.clear() method (just pop() until an IndexError is raised), some documentation updates and a bunch of unit tests. Christian Index: Objects/listobject.c =================================================================== --- Objects/listobject.c (revision 71106) +++ Objects/listobject.c (working copy) @@ -785,6 +785,14 @@ } static PyObject * +listclear(PyListObject *self, PyObject *unused) +{ + if (list_resize(self, 0) == 0) + Py_RETURN_NONE; + return NULL; +} + +static PyObject * listextend(PyListObject *self, PyObject *b) { PyObject *it; /* iter(v) */ @@ -2458,6 +2466,8 @@ "L.__sizeof__() -- size of L in memory, in bytes"); PyDoc_STRVAR(append_doc, "L.append(object) -- append object to end"); +PyDoc_STRVAR(clear_doc, +"L.clear() -- clear the list"); PyDoc_STRVAR(extend_doc, "L.extend(iterable) -- extend list by appending elements from the iterable"); PyDoc_STRVAR(insert_doc, @@ -2486,6 +2496,7 @@ {"__reversed__",(PyCFunction)list_reversed, METH_NOARGS, reversed_doc}, {"__sizeof__", (PyCFunction)list_sizeof, METH_NOARGS, sizeof_doc}, {"append", (PyCFunction)listappend, METH_O, append_doc}, + {"clear", (PyCFunction)listclear, METH_NOARGS, clear_doc}, {"insert", (PyCFunction)listinsert, METH_VARARGS, insert_doc}, {"extend", (PyCFunction)listextend, METH_O, extend_doc}, {"pop", (PyCFunction)listpop, METH_VARARGS, pop_doc},

Andre Roberge <andre.roberge@gmail.com> writes:
I wouldn't think it worth a PEP. Simpler to produce a patch implementing and documenting the change, and see what reception it gets on python-dev. -- \ “I got an answering machine for my phone. Now when someone | `\ calls me up and I'm not home, they get a recording of a busy | _o__) signal.” —Steven Wright | Ben Finney
participants (10)
-
Andre Roberge
-
Ben Finney
-
Christian Heimes
-
George Sakkis
-
Gerald Britton
-
Giampaolo Rodola'
-
Mark Summerfield
-
Michael Foord
-
Paige Thompson
-
Raymond Hettinger