6 ms·
> Take lists: some operations are methods, some are functions, some mutate the list, some make a copy, some are global, some are in a module. Let's take the li
by rciorba 8y ago
> Take lists: some operations are methods, some are functions, some mutate the list, some make a copy, some are global, some are in a module.
Let's take the list type, the public methods are: 'clear', 'copy', 'count', 'extend', 'index', 'insert', 'pop', 'remove', 'reverse', 'sort'
Obviously as the names imply `copy` returns a new list, and `count` returns an integer. Neither mutate the list.
For the rest of it: methods mutate the list. There are some builtin functions that do similar things, but those always a new object (sorted, reversed)
- colanderman 8y agoOK, fair point. I misremembered the list interface. `set` is a better example: `set.union`, for example, returns a new set, while `set.add` updates in place. (To be fair, this is with Python 2.7, maybe Python 3+ is more uniform?)
- acdha 8y agoWhat would you expect it to do differently in those cases? That seems like the API is following how most people would think about those operations as you'd expect in a language which isn't trying to be immutable.
- pfranz 8y agoI thought it was interesting that Ruby, by convention, used an exclamation mark to convey that a function would mutate. I haven't used Ruby all that much to see how well that works at scale.
- deleted 8y ago[deleted]
- pseudalopex 8y agoMany libraries don't use it. Another convention uses it for methods that might raise an exception.