Skip to content

How can I extract the start, stop and step values from a py::slice object? #4716

Closed Answered by 123epsilon
123epsilon asked this question in Q&A
Discussion options

You must be logged in to vote

Solution:

auto parseAttr = [](const py::object &obj) -> OptionalIntValue {
  if(py::isinstance<py::none>(obj)){
    return py::none();
  }
  else if(py::isinstance<py::int_>(obj)){
    return obj.cast<int>();
  }
  else {
    throw std::invalid_argument("Invalid indices);
  }
  };
        
// py::isinstance<py::slice>(sliceObj) == true
parseAttr(getattr(sliceObj, "start"));

Replies: 1 comment 2 replies

Comment options

You must be logged in to vote
2 replies
@reubenjohn
Comment options

@123epsilon
Comment options

Answer selected by 123epsilon
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Category
Q&A
Labels
None yet
2 participants