Proper Python Syntax and Semantics: if, else, pass [duplicate] - python

This question already has answers here:
Using pass on a non necessary else statement
(5 answers)
Closed 8 years ago.
Is there a preferred/proper style?
This:
def fx(Boolean):
if Boolean:
# Do stuff.
else:
pass
Or this:
def fx(Boolean):
if Boolean:
# Do stuff.
Is it preferred/proper to include else: pass if you don't want anything to happen?
I've read PEP 8 - Style Guide for Python Code and did not find anything concerning my question.

You should never include else: pass. It's superfluous. Just omit the else; it's intentionally an optional keyword.

If you don't need the else if there is no reason to add it. It will just confuse other people reading your code in the future (ie yourself a few months later).

Related

Python conditional return [duplicate]

This question already has answers here:
Python Ternary Operator Without else
(8 answers)
Conditional return with no else
(2 answers)
Closed last month.
Quite frequently, I have written lines like
if arg == foo: return bar
It is naturally a one-liner, at the beginning of a function body. Notice there is no else, it just returns on a special value of the parameter, and proceeds with the normal flow of the function otherwise.
Still, it feels like the order is wrong. In perl it is possible to write (modulo some $'s)
return bar if arg == foo
which feels more natural. Matter of taste, I know.
Is there a pythonic way of writing something after a return word that would impose a condition on the return statement?
It is, of course, possible, that there is no way.

what does "..." mean and difference between ... and pass in python? [duplicate]

This question already has answers here:
What does the Ellipsis object do?
(14 answers)
Closed 3 years ago.
Well...When I define a function that does not perform certain actions I don't understand what the difference between ... and pass. For example, the following two pieces of code:
def _clear() -> None: ...
def _clear() -> None: pass
They are the same, right? Either one can be used? I sincerely hope someone can help me thoroughly understand it. Thanks...
pass is the standard way to say that a function does not do anything.
Placing the ellipsis object does yield valid code just like def _clear() -> None: x = 5, but most IDEs and static analysis tools will complain that the statement has no effect.

Shouldn't "a:1" be a syntax error in python? [duplicate]

This question already has answers here:
What is this odd colon behavior doing?
(2 answers)
Closed 3 years ago.
I made a typo in my code that went completely silent syntactically.
dict_args : {"arg1":1,"arg2":2,"arg3":3}
# .... Some more code
some_function(**dict_args)
# .... Some more code
If you haven't noticed it, it's the use of : instead of = when declaring the variable dict_args.
So my question is, does the python syntax : a:1, by itself, hold any meaning ? Or should it hypothetically be considered a syntax error?
PEP-526 introduced variable annotations, which provide programmers a way to add type information to variables. This allows, among other things, statements like
x: int
to indicate that there is a local variable of type int, without initializing it. In PEP-484 - Acceptable Type Hints, we can see that annotations "must be valid expressions that evaluate without raising exceptions", which your dictionary literal is.
If you look at the Python grammar itself you can see that the expr_stmt and annassign rules make the example you show legal.
If you're using an IDE/other type hinting tools, they should definitely complain about this, but it doesn't break the rules that Python has set up.

When to use If If over an If Elif in Python [duplicate]

This question already has answers here:
If and elif in Python for good programming practices
(2 answers)
Closed 4 years ago.
Does someone have a simple answer as to why you would use a If If rather than an If Elif?
Example:
def fun(item):
if item[0]<'M':
return 0
if item[0]<'Q':
return 1
return 2
With a setup like this, where you are returning a value inside each if block, the if statements behave exactly the same as an if elif structure since the return exits out of the function without checking the conditions below. So, when the functionality is the same, the choice should be made on readability. explicit is better than implicit.
There is no good reason to use if statements like this implying exclusively in the conditionals, when if elif statements makes the exclusivity explicit and is much more readable and easier to maintain.

What does the term pass mean in python? [duplicate]

This question already has answers here:
How to use "pass" statement?
(18 answers)
Closed 6 years ago.
What does pass mean in python? I have seen it used and I do not see why you need to use it or what it does? I guess I could say it passes over whatever function it is in, but why include it?
Quoth the doc:
The pass statement does nothing. It can be used when a statement is required syntactically but the program requires no action.
References:
https://docs.python.org/2/reference/simple_stmts.html#the-pass-statement
https://docs.python.org/2/tutorial/controlflow.html#pass-statements
"It is used when a statement is required syntactically but you do not want any command or code to execute.
The pass statement is a null operation; nothing happens when it executes. The pass is also useful in places where your code will eventually go, but has not been written yet (e.g., in stubs for example):"
https://www.tutorialspoint.com/python/python_pass_statement.htm

Categories

Resources