[Flask] Shouldn't is and == operator work same way while doing string comparisons?
Arup Rakshit
ar at zeit.io
Fri May 3 14:58:38 EDT 2019
Hi,
I am facing a problem with `is` and `==` comparisons. The small below program gives different outputs. When I use `if book['isbn'] is isbn]` I get an empty output, but `if book['isbn'] == isbn]` gives correct output. Why it is like that?
from flask import Flask, jsonify
app = Flask(__name__)
books = [
{
"name": 'Learning Python',
'isbn': '1449355730',
'price': 200
},
{
"name": 'Practical Object-Oriented Design: An Agile Primer Using Ruby',
'isbn': 'B07F88LY9M',
'price': 210
}
]
# ….
@app.route('/books/<isbn>')
def get_book_by_isbn(isbn):
return jsonify([{'name': book['name'], 'price': book['price']} for book in books if book['isbn'] == isbn])
if __name__ == "__main__":
app.run(debug=True)
Curl program:
~$ curl http://127.0.0.1:5000/books/B07F88LY9M
[]
~$ curl http://127.0.0.1:5000/books/B07F88LY9M
[
{
"name": "Practical Object-Oriented Design: An Agile Primer Using Ruby",
"price": 210
}
]
Thanks,
Arup Rakshit
ar at zeit.io
More information about the Flask
mailing list