3 ms·
random.shuffle() has bitten me that way a few times too: array = random.shuffle(array) because I expected it to return a copy or reference, instead making
by aftbit 5y ago
random.shuffle() has bitten me that way a few times too:
array = random.shuffle(array)
because I expected it to return a copy or reference, instead making my array None.
It would also enable chaining operations:
array = array.append(A).append(B).sort()
In-place vs immutable copy is a language design choice with tradeoffs on both sides, but there's no reason that I can see to not return a reference to the list.
Perhaps recognizing this is really the job of an external linter. Sometimes I wonder if the future of enforcing canonical formatting on save like "gofmt" or "black" will extend to auto-correcting certain goofy errors on each save.
mypy would yell at you about this, but afaik type-checked python still isn't the norm.
- int_19h 5y agoIn Python, a function that makes and returns a copy would be idiomatically named shuffled(). Consider sorting: xs.sort() # in-place ys = sorted(xs) # copy As for functions returning the object - I think it's a hack around the absence of direct support for such repetition in the language itself. E.g. in Object Pascal, you'd write: with array do begin append(A); append(B); sort; end; Or better yet, in Smalltalk: array append(A); append(B); sort. https://en.wikipedia.org/wiki/Method_cascading https://en.wikipedia.org/wiki/Method_cascading
- igouy 5y agoAlso https://dart.dev/guides/language/language-tour#cascade-notation https://dart.dev/guides/language/language-tour#cascade-notat...