#๐Ÿ”’ Which is faster (or preferred on work groups)?

14 messages ยท Page 1 of 1 (latest)

sour goblet
#

This code:

def function(): if something: code else: code

Or this one:

def function(): if something: code return code

prime impBOT
#

@sour goblet

Python help channel opened

Remember to:

  • Ask your Python question, not if you can ask or if there's an expert who can help.
  • Show a code sample as text (rather than a screenshot) and the error message, if you've got one.
  • Explain what you expect to happen and what actually happens.

:warning: Do not pip install anything that isn't related to your question, especially if asked to over DMs.

rotund merlin
#

The second if good if you have a lot of nested ifs

unkempt arch
rotund merlin
#

If you have only a few ifs, the first one is more readable

#

They are probably as fast

#

(You could try to measure it tho)

scenic geyser
#

Fundamentally, it depends on the nature of something, and the amount of the second code

sour goblet
#

Isnโ€™t something always a bool value?

#

And why the amount of the second code is relevant?

humble wave
#

the first form can simplify the following code and reduce nesting. It's appropriate for filtering out exceptional conditions or special cases. You can deal with them and then return early, and then the rest of your code knows that the input data is in a good state and it can focus on that happy path.

scenic geyser
#

Just knowing something evaluates to (or can be interpreted as!) a boolean, doesn't explain its purpose.

prime impBOT
#
Python help channel closed for inactivity

This help channel has been closed. Feel free to create a new post in #1035199133436354600. To maximize your chances of getting a response, check out this guide on asking good questions.