2

So here's a simple object creation and assignment using a 1 line ternary expression in Java.

Interval newInterval = previous.end <= current.end ? new Interval(previous.start, current.end : new Interval(previous.start, previous.end)

The python equivalent is,

new_interval = Interval(previous.start, current.end) if previous.end <= current.end else Interval(previous.start, previous.end)

My question is there a more pythonic way to write this?

1
  • 1
    new_interval = Interval(previous.start, current.end if previous.end <= current.end else previous.end) seems more reasonable to me (in both languages). Commented Jul 18, 2017 at 23:05

2 Answers 2

6

The form I'd like more is probably

new_interval = Interval(previous.start, max(current.end, previous.end))
Sign up to request clarification or add additional context in comments.

Comments

2

This is more Pythonic:

start = previous.start
end = max(current.end, previous.end)
new_interval = Interval(start, end)

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.