Back to top
Edit this page
Toggle table of contents sidebar
Message emitted:
Consider merging these comparisons with 'in' by using '%s %sin (%s)'. Use a set instead if elements are hashable.
Description:
To check if a variable is equal to one of many values, combine the values into a set or tuple and check if the variable is contained «in» it instead of checking for equality against each of the values. This is faster and less verbose.
Problematic code:
def fruit_is_round(fruit): # +1: [consider-using-in] return fruit == "apple" or fruit == "orange" or fruit == "melon"
Correct code:
def fruit_is_round(fruit): return fruit in {"apple", "orange", "melon"}
Created by the refactoring checker.
Permalink
Cannot retrieve contributors at this time
R1714 (consider-using-in)
❌ Problematic code:
array = list(range(10)) n = input() for elem in array: if int(n) == elem: pass
✔️ Correct code:
array = list(range(10)) n = input() if int(n) in array: pass
Rationale:
To check if a variable is equal to one of many values,combine the values into
a tuple and check if the variable is contained in
it instead of checking
for equality against each of the values. This is faster and less verbose.
Related resources:
- Issue Tracker
My Code :
def modify_book():
db = mysql.connector.connect(host='localhost',
database='library',
user='Aishwary_Pandey',
password='aishwary')
c = db.cursor()
clear()
print('M O D I F Y -- B O O K -- D E T A I L S')
print('-'*120)
print('n1. Book Title')
print('n2. Book Author')
print('n3. Book Publisher')
print('n4. Book Pages')
print('n5. Book Price')
print('n6. Book Edition')
print('n7. Exit')
print('nn')
choice = int(input('Enter your choice : '))
field = ''
if choice == 1:
field = 'title'
if choice == 2:
field = 'author'
if choice == 3:
field = 'publisher'
if choice == 4:
field = 'pages'
if choice == 5:
field = 'price'
if choice == 6:
field = 'edition'
book_id = input('Enter ID of the Book : ')
value = input('Enter the Value to be Updated : ')
if field == 'pages' or field == 'price':
sql = 'update book set ' + field + ' = '+value+' where id = '+book_id+';'
else:
sql = 'update book set ' + field + ' = "'+value+'" where id = '+book_id+';'
error :
basically there is no error but my idle (spyder) suggested me to merge these comparisons with «in» to «field in (‘pages’, ‘price’)» which I am not able to understand how would I do that.
please help.
asked Mar 5, 2021 at 6:57
This should «fix» it
if field in ('pages', 'price'):
sql = 'update book set ' + field + ' = '+value+' where id = '+book_id+';'
else:
sql = 'update book set ' + field + ' = "'+value+'" where id = '+book_id+';'
answered Aug 25, 2021 at 13:17
Permalink
Cannot retrieve contributors at this time
consider-using-in (R1714)
Consider merging these comparisons with ‘in’ by using ‘%s %sin (%s)’.
Use a set instead if elements are hashable. To check if a variable is
equal to one of many values, combine the values into a set or tuple and
check if the variable is contained «in» it instead of checking for
equality against each of the values. This is faster and less verbose.
-
- GitLab: the DevOps platform
- Explore GitLab
- Install GitLab
- How GitLab compares
- Get started
- GitLab docs
- GitLab Learn
- Pricing
- Talk to an expert
-
/
-
Help
- Help
- Support
- Community forum
- Submit feedback
- Contribute to GitLab
- Switch to GitLab Next
-
Projects
Groups
Topics
Snippets -
Register - Sign in
pylint-errors
Project ID: 19126888
Star
0
Topics:
pylint
pylint-errors
linter
A curated list of pylint errors with explanation and examples
Find file
Download source code
tar.gz
tar.bz2
tar
Clone
-
Clone with SSH
-
Clone with HTTPS
-
Open in your IDE
Visual Studio Code (SSH)Visual Studio Code (HTTPS)IntelliJ IDEA (SSH)IntelliJ IDEA (HTTPS)
- Copy SSH clone URLgit@gitlab.com:britonad/pylint-errors.git
- Copy HTTPS clone URLhttps://gitlab.com/britonad/pylint-errors.git
- README
- MIT License