Python Style Guide Questions - Contd.

K

koranthala

Hi,
I have some more questions about python code styling.
1. Global Variables: In my code, I am using some global variables.
Now, when I ran PyLint, it raised convention errors mentioning that
they should be CAPITAL_ALPHABETS. Now, in PEP 8, I did not see that
mentioned. Please let me know whether that is the usual styling
mechanism which is used.
2. I have many loops wherein I define a variable as just a counter.
for x in range(counter):
do_something()
Please note that I am not using x anywhere in the program.
Pylint again raises warnings saying that the variable should be
used.
Should I disregard it or use the default variable _ ?
for _ in range(counter):
do_something()
pylint does not raise errors for those. Are there any issues in
using _ ?
 
D

Diez B. Roggisch

koranthala said:
Hi,
I have some more questions about python code styling.
1. Global Variables: In my code, I am using some global variables.
Now, when I ran PyLint, it raised convention errors mentioning that
they should be CAPITAL_ALPHABETS. Now, in PEP 8, I did not see that
mentioned. Please let me know whether that is the usual styling
mechanism which is used.

I adopted that convention, however I don't know if it is "official". I'd
consider it good style though.
2. I have many loops wherein I define a variable as just a counter.
for x in range(counter):
do_something()
Please note that I am not using x anywhere in the program.
Pylint again raises warnings saying that the variable should be
used.
Should I disregard it or use the default variable _ ?
for _ in range(counter):
do_something()
pylint does not raise errors for those. Are there any issues in
using _ ?

Nope, using _ is perfectly fine for variables you don't care about. Another
example is e.g.

foo, bar, _ = ("a", "b", "c")

Diez
 
M

Marc 'BlackJack' Rintsch

Hi,
I have some more questions about python code styling. 1. Global
Variables: In my code, I am using some global variables.
Now, when I ran PyLint, it raised convention errors mentioning that they
should be CAPITAL_ALPHABETS. Now, in PEP 8, I did not see that
mentioned. Please let me know whether that is the usual styling
mechanism which is used.

PEP8 doesn't mention constants at all. The all caps naming for constants
is a convention in several languages.
2. I have many loops wherein I define a variable as just a counter.
for x in range(counter):
do_something()
Please note that I am not using x anywhere in the program. Pylint
again raises warnings saying that the variable should be
used.
Should I disregard it or use the default variable _ ? for _ in
range(counter):
do_something()
pylint does not raise errors for those. Are there any issues in
using _ ?

Pylint doesn't barf on `dummy` either. The point is having a name that
makes clear at the head of the loop, that the reader doesn't have to
bother looking for places where the value will be used because it is
clear from the name that the value won't be used at all.

BTW pylint is very configurable, you can dump the default configuration
and find out which names are "allowed" by default and of course define
your own set of "value doesn't matter" names.

Ciao,
Marc 'BlackJack' Rintsch
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

No members online now.

Forum statistics

Threads
473,744
Messages
2,569,484
Members
44,903
Latest member
orderPeak8CBDGummies

Latest Threads

Top