python needs leaning stuff from other language

A

Armin

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

Emile said:
Esmail wrote:
Diez B. Roggisch wrote:
(e-mail address 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

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

del mylist[:]
 
O

online.service

python's list needs a thing list.clear() like c# arraylist
and
python needs a writeline() method
 
L

Luis Alberto Zarrabeitia Gomez

Quoting (e-mail address removed):
python's list needs a thing list.clear() like c# arraylist

It has:
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:
 
L

Luis Alberto Zarrabeitia Gomez

Quoting Esmail said:
Diez said:
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).
 
E

Emile van Sebille

Esmail said:
Diez said:
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
 
G

Gary Herron

python's list needs a thing list.clear() like c# arraylist
and
python needs a writeline() method

(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
 
E

Esmail

Emile said:
Esmail said:
Diez said:
(e-mail address 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
 
Z

Zac Burns

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



Emile said:
Diez B. Roggisch wrote:

(e-mail address 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
 
M

MRAB

Zac said:
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.
 
S

Steven D'Aprano

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.
 
Z

Zamnedix

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.
 
M

Mel

Ben said:
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.
 
S

Steven D'Aprano

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.
 
M

Mel

Steven said:
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.
 
T

Tim Wintle

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
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

Forum statistics

Threads
473,744
Messages
2,569,484
Members
44,903
Latest member
orderPeak8CBDGummies

Latest Threads

Top