What does this use of Python 'or' operator do? [duplicate] - python

This question already has answers here:
Boolean Python Value confusion
(2 answers)
How do "and" and "or" act with non-boolean values?
(8 answers)
Closed 4 years ago.
I came across this piece of Python code. The precise function is not important. I'm trying to understand how the arguments to the json.dumps call work. It looks (at a guess) like the intent is to use either body or kwargs['body'] depending on which one is non-null, but doesn't the or operator just return a boolean?
import json
from aiohttp import web
def json_response(body='', **kwargs):
kwargs['body'] = json.dumps(body or kwargs['body']).encode('utf-8')
kwargs['content_type'] = 'text/json'
return web.Response(**kwargs)

Related

Control flow in python with strings [duplicate]

This question already has answers here:
Python 2.7 Boolean Operators Logic
(1 answer)
How does boolean operator work on string in python
(6 answers)
Closed 2 years ago.
I have a very basic snippet of code.
print("a" or "b")
>> a
I'm not entirely sure what I expected to happen.
Why does this print "a", and in general how are strings handled with python control flow?
In python, empty strings evaluate to False in a boolean context, while non-empty strings are True.

Is it possible to create a statement in python? [duplicate]

This question already has answers here:
Can you add new statements to Python's syntax?
(13 answers)
Add new statements to Python without customizing the compiler
(2 answers)
Closed 4 years ago.
Can I create a statement in python 2.7?
Something similar to the print statement, that it's like a function, but gets the parameters without parenthesis.

Python, can someone explain why the return statement only prints the first object? [duplicate]

This question already has answers here:
What does return mean in Python? [closed]
(2 answers)
Why is "None" printed after my function's output?
(7 answers)
Python: Why "return" won´t print out all list elements in a simple for loop and "print" will do it?
(4 answers)
Closed 5 years ago.
I have been practising python and have a small question. I am working with DNA sequences and so I simply wanted to make a small function that just returned the record.ids.
from Bio import AlignIO
my_alignment = Align.IO.read("multipleseqfile.fa","fasta")
def get_id_names(alignment):
for record in alignment:
return record.id
print get_id_names(my_alignment)
I had done a for loop before that prints the names nicely but I wanted to improve my script and make these exercises into functions. However, when I use this function, it only returns the first record id (and there is a list of 30-40). I switched the return record.id to print record.id, and it does print all the names but then I get a None at the end of the output. Not sure what is going on here?

python - does a function in the right side of a conditional `and` run when the left side fails? [duplicate]

This question already has answers here:
Does Python support short-circuiting?
(3 answers)
Closed 5 years ago.
consider this:
valid = data and func(data)
In the cases where data will be False, will python still run the function on the right side of the and condition? (it will error if run, so I'm trying to figure out if I need to check data within func or is the above code fine as is)
Well, it's easy to try:
>>> 0 and False
0
so the answer is: python will not run func(data) if data is False.

Difference between != and <>? [duplicate]

This question already has answers here:
What does `<>` mean in Python?
(5 answers)
Closed 8 years ago.
I just saw some code using the <> operator (don't know what this is called) instead of the != operator in Python. Is there any difference between the two or do they mean the same thing? What's the <> operator called? Thanks.
The <> operator is considered obsolete:
https://docs.python.org/2/reference/expressions.html#not-in

Categories