Why does Python list use append, not push?

Asked 2 years ago, Updated 2 years ago, 23 views

We already have list.pop(), so if you think about it, isn't it right to say list.push(), not list.append(), right?

I wonder why you use it as list.append()! Why?

python

2022-09-22 15:16

1 Answers

You can see it from Python history.

This is because list.append was invented in 1991, while pop was invented in 1997.

Python founder Guido also thought push was more appropriate for pop, but he didn't want to name the method that does the same thing differently, so the append keeps surviving.

To implement a stack, one would need to add a list.pop() primitive (and no, I'm not against this particular one on the basis of any principle). list.push() could be added for symmetry with list.pop() but I'm not a big fan of multiple names for the same operation -- sooner or later you're going to read code that uses the other one, so you need to learn both, which is more cognitive load.


2022-09-22 15:16

If you have any answers or tips


© 2024 OneMinuteCode. All rights reserved.