python - How to write a function which takes a slice? -
i write function in python takes slice parameter. ideally user able call function follows:
foo(a:b:c)
unfortunately, syntax not permitted python - use of a:b:c
allowed within []
, not ()
.
i therefore see 3 possibilities function:
require user use slice "constructor" (where
s_
acts the version provided numpy):foo(slice(a, b, c)) foo(s_[a:b:c])
put logic of function
__getitem__
method:foo[a:b:c]
give trying take slice , take start, stop , step individually:
foo(a, b, c)
is there way original syntax work? if not, of workaround syntaxes preferred? or there another, better option?
don't surprise users.
if use slicing syntax consistently developer expects slicing syntax, same developer expect square brackets operation, i.e. __getitem__()
method.
if instead returned object not somehow slice of original object, people confused if stick __getitem__()
solution. use function call foo(a, b, c)
, don't mention slices @ all, , optionally assign default values if makes sense.
Comments
Post a Comment