-1

Why operations such as

[1, 2].append(3)
{1, 2, 3}.remove(2)

silently return None in Python with no diagnostics? So one has to use variables like

a = [1, 2]
a.append(3)

How can that be explained?

Viktor
  • 472
  • 5
  • 14
  • Those methods "silently return None" in all situations - applying them to an inline-defined value makes no difference. – jasonharper Aug 14 '23 at 03:11
  • 1
    It's a design choice, that's all. `append` could return the list being modified, but they chose not to, in part because it encourages bad practices. – Tim Roberts Aug 14 '23 at 03:12
  • The design philosophy is that methods that mutate should not also return the mutated object. That creates ambiguities when the return is assigned to a different variable. Look at things that chain like pandas - they have significant problems with copies and views. There is also a small benefit in not doing an extra addref / decref cycle for each. – tdelaney Aug 14 '23 at 03:41
  • @tdelaney. addref/decref isn’t the issue. You always have to deal with the return value of a function, even if you’re ignoring it and you know it’s None. – Frank Yellin Aug 14 '23 at 03:47
  • @FrankYellin - the latest pythons will start optimizing ref counting `None`. – tdelaney Aug 14 '23 at 03:55
  • "How can that be explained?" Nothing needs explaining. And I don't know what you mean by "silently" and without "diagnostics". – juanpa.arrivillaga Aug 14 '23 at 04:42
  • @tdelaney do you have a link for a PEP about that? – juanpa.arrivillaga Aug 14 '23 at 04:44
  • @juanpa.arrivillaga It was in an article by a python dev about performance boosts along with other teasers like reducing the impact of the GIL. If I can find it again, I'll post. – tdelaney Aug 14 '23 at 14:19

0 Answers0