serhii.net

In the middle of the desert you can say anything you want

20 Dec 2021

python None in slice notation

After for the nth time writing awkward code like

if limit is None: 
    limit = len(mylist)

decided to see if there’s a better way. Looked into the walrus operator etc,but decided to test what I get with None.

Well, mylist[:None] works! No errors, I’d guess I get a copy of it same as mylist[:].

Will save me hundreds of lines in the future!

Docu about slice1 is terse, says it uses range(start,end,step) under the hood with start and step defaulting to None. But range doesn’t accept None for all arguments! TODO for later I guess.

Nel mezzo del deserto posso dire tutto quello che voglio.
comments powered by Disqus