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

Dan Stromberg drsalists at gmail.com
Fri Nov 25 10:56:22 EST 2022


On Sun, Nov 13, 2022 at 4:45 PM DFS <nospam at dfs.com> wrote:

> 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://mail.python.org/mailman/listinfo/python-list



I'm not 100% sanguine about properties, but the fact is they are part of
the language:

$ cat p
below cmd output started 2022 Fri Nov 25 07:54:42 AM PST
#!/usr/bin/env python3

class P:
    def __init__(self):
        self.count = 0

    @property
    def increment(self):
        self.count += 1

    def result(self):
        return self.count


p = P()
p.increment
p.increment
print(p.result())
above cmd output done    2022 Fri Nov 25 07:54:42 AM PST
dstromberg at tp-mini-c:~/src/experiments/property x86_64-pc-linux-gnu 2670

$ ./p
below cmd output started 2022 Fri Nov 25 07:54:44 AM PST
2

As you can see, if the interpreter refused to do something with p.increment
because it has no parens, the meaning of this code would change
significantly.


More information about the Python-list mailing list