In code, list.clear doesn't throw error - it's just ignored

Weatherby,Gerard gweatherby at uchc.edu
Mon Nov 14 06:35:26 EST 2022


The terminal told you what x.clear was.

The Python documentation tells you how to call it: https://docs.python.org/3/tutorial/datastructures.html

list.clear()
Remove all items from the list. Equivalent to del a[:].

An IDE (e.g. PyCharm) will try to autocomplete the parentheses and warn you if you don’t:

“Statement seems to have no effect and can be replaced with a function call to have effect”

From: Python-list <python-list-bounces+gweatherby=uchc.edu at python.org> on behalf of DFS <nospam at dfs.com>
Date: Sunday, November 13, 2022 at 7:46 PM
To: python-list at python.org <python-list at python.org>
Subject: In code, list.clear doesn't throw error - it's just ignored
*** Attention: This is an external email. Use caution responding, opening attachments or clicking on links. ***

In code, list.clear is just ignored.
At the terminal, list.clear shows
<built-in method clear of list object at 0x000001C9CFEC4240>


in code:
x = [1,2,3]
x.clear
print(len(x))
3

at terminal:
x = [1,2,3]
x.clear
<built-in method clear of list object at 0x000001C9CFEC4240>
print(len(x))
3


Caused me an hour of frustration before I noticed list.clear() was what
I needed.

x = [1,2,3]
x.clear()
print(len(x))
0

--
https://urldefense.com/v3/__https://mail.python.org/mailman/listinfo/python-list__;!!Cn_UX_p3!jRrjG0fRo46VyY0jLfD1Z5C6tXDiphZy8zi2AqN_N5BB1_OwBe_wxmsBWmIVOFQDdZnvbRq1JNeGnPg$<https://urldefense.com/v3/__https:/mail.python.org/mailman/listinfo/python-list__;!!Cn_UX_p3!jRrjG0fRo46VyY0jLfD1Z5C6tXDiphZy8zi2AqN_N5BB1_OwBe_wxmsBWmIVOFQDdZnvbRq1JNeGnPg$>


More information about the Python-list mailing list