Reputation: 2607
To retrieve a sub range of elements of an array, we can write myData[from : to]
syntax.
I have a function, the simplified variant looks like this:
def myFunction(fromIndex, toIndex):
return myData[fromIndex: toIndex]
Let's assume I can't / don't want to change the behavior of myFunction
, but in some cases I want to get the whole array (ie. in theory fromIndex = 0, toIndex = len(myData)
. I can write x = myFunction(0, len(myData))
, but in this case I have to know what ˙myData˙ means.
Is there any other way? Something like x = myFunction(, )
, which would expand to return myData[ : ]
and return the (copy of the) whole array. Does this range-indexing syntax has some special symbols? myData[0 : 0 or None] == myData
?
Upvotes: 1
Views: 217
Reputation: 6633
One more but similar
myData = [ 1, 3, 5, 7, 9 ]
def myFunction(fromIndex=None, toIndex=None):
return myData[fromIndex: toIndex]
print(myFunction(fromIndex=0))
Upvotes: 0
Reputation: 15738
Use None
values for either/both of the argument defaults. Natively, some_list[None:None]
returns the whole list.
def myFunction(fromIndex=None, toIndex=None):
return myData[fromIndex: toIndex]
# myFunction() will return the whole list
Upvotes: 2