Velocity Reviews - Computer Hardware Reviews

Velocity Reviews > Newsgroups > Programming > Python > python needs leaning stuff from other language

Reply
Thread Tools

python needs leaning stuff from other language

 
 
Armin
Guest
Posts: n/a
 
      04-02-2009
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 <(E-Mail Removed)> wrote:
> > Emile van Sebille wrote:
> >> Esmail wrote:
> >>> Diez B. Roggisch wrote:
> >>>> http://www.velocityreviews.com/forums/(E-Mail Removed) 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
 
Reply With Quote
 
 
 
 
online.service@ymail.com
Guest
Posts: n/a
 
      04-02-2009
python's list needs a thing list.clear() like c# arraylist
and
python needs a writeline() method

 
Reply With Quote
 
 
 
 
Diez B. Roggisch
Guest
Posts: n/a
 
      04-02-2009
(E-Mail Removed) schrieb:
> python's list needs a thing list.clear() like c# arraylist
> and


some_list[:] = []

> python needs a writeline() method


print()

Diez
 
Reply With Quote
 
Esmail
Guest
Posts: n/a
 
      04-02-2009
Diez B. Roggisch wrote:
> (E-Mail Removed) 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?)?

 
Reply With Quote
 
Esmail
Guest
Posts: n/a
 
      04-02-2009
Diez B. Roggisch wrote:
> (E-Mail Removed) 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?)?

 
Reply With Quote
 
Luis Alberto Zarrabeitia Gomez
Guest
Posts: n/a
 
      04-02-2009

Quoting (E-Mail Removed):

> 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

 
Reply With Quote
 
Luis Alberto Zarrabeitia Gomez
Guest
Posts: n/a
 
      04-02-2009

Quoting Esmail <(E-Mail Removed)>:

> 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

 
Reply With Quote
 
Emile van Sebille
Guest
Posts: n/a
 
      04-02-2009
Esmail wrote:
> Diez B. Roggisch wrote:
>> (E-Mail Removed) 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
>


 
Reply With Quote
 
Gary Herron
Guest
Posts: n/a
 
      04-02-2009
(E-Mail Removed) 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



 
Reply With Quote
 
Esmail
Guest
Posts: n/a
 
      04-03-2009
Emile van Sebille wrote:
> Esmail wrote:
>> Diez B. Roggisch wrote:
>>> (E-Mail Removed) 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

 
Reply With Quote
 
 
 
Reply

Thread Tools

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is On
HTML code is Off
Trackbacks are On
Pingbacks are On
Refbacks are Off


Similar Threads
Thread Thread Starter Forum Replies Last Post
Left leaning red black trees Antoninus Twink C Programming 10 12-03-2009 01:31 PM
Leaning out Vista 64 a little? Don Windows 64bit 5 02-24-2008 11:18 PM
Is there any provisions for leaning difficulties in the mcse exams =?Utf-8?B?ZW5ncmhhcnY=?= Microsoft Certification 1 04-14-2005 10:02 AM
Long trip - Leaning towards Coolpix 8800 - PLEASE ADVISE! ggal Digital Photography 3 03-28-2005 11:26 PM
MONA LISA AND THE LEANING TOWER OF PIZZI Bendt Ten Brock Digital Photography 0 01-19-2004 11:32 AM



Advertisments