Question about PEP 8

L

Laszlo Nagy

Hi All,

Here is what I read in PEP 8:
Package and Module Names

Modules should have short, all-lowercase names. Underscores can be used
in the module name if it improves readability. Python packages should
also have short, all-lowercase names, although the use of underscores is
discouraged.

Since module names are mapped to file names, and some file systems are
case insensitive and truncate long names, it is important that module
names be chosen to be fairly short -- this won't be a problem on Unix,
but it may be a problem when the code is transported to older Mac or
Windows versions, or DOS.

When an extension module written in C or C++ has an accompanying Python
module that provides a higher level (e.g. more object oriented)
interface, the C/C++ module has a leading underscore (e.g. _socket).

Here is my problem. There is ConfigParser, StringIO, Queue, HTMLParser
etc. They are all part of the standard library. Most of these are
modules with only a "main class" defined. ConfigParser module contains
ConfigParser class, Queue module contains Queue class etc. Should I use
CapWords for the module (and file) name or not? E.g. should I

from mess.wxmegaeidgets.GenericDialog import GenericDialog

or

from mess.wxmegaeidgets.genericdialog import GenericDialog


(suppose that the module contains only the GenericDialog class. BTW,
will Py3K change the standard library names in order to follow PEP 8?)

I also have problems with function and method names:
Function Names

Function names should be lowercase, with words separated by underscores
as necessary to improve readability.

mixedCase is allowed only in contexts where that's already the
prevailing style (e.g. threading.py), to retain backwards compatibility.

Great, but what if I subclass wx widgets? They all have CapWords.
(wx.Window.ShowModal, wx.Window.Bind etc.) I must use CapitalizedWords
for method names because sometimes I have to override inherited methods,
and it is not possible to change their name. Where should be the limit?
If I create a composite widget then should I use lowercase and
underscores? If I create a class that is not itself a widget, but works
with widgets? The PEP defines the "theoretically good" coding style, but
what is the correct style in an environment where you have third party
libraries that are not following the PEP?

Thanks,

Laszlo
 
M

Matimus

will Py3K change the standard library names in order to follow PEP 8?)

Just continue down the list of PEPs and you will find the answer:

http://www.python.org/dev/peps/pep-3108/#modules-to-rename

This covers built-in modules. It doesn't cover 3rd party modules, like
wx. Most likely you will still see pep8 violations in non-standard
modules.

As for following or not following pep8 in conjunction with 3rd party
modules. Just make your code look nice. Don't go out of your way to
follow pep8 if it means that there will be a mix of styles that will
be confusing. If you are overloading a class that is part of wx I
would use the same style that wx uses. The reasoning being that people
who might use that class would probably see it as just another wx
class, and expect it to look and behave a certain way. Don't
dissapoint them.

Matt
 
B

Ben Finney

Laszlo Nagy said:
Here is my problem. There is ConfigParser, StringIO, Queue,
HTMLParser etc. They are all part of the standard library.

Yes, many modules in the standard library don't conform to PEP 8;
these are known bugs.
Most of these are modules with only a "main class"
defined. ConfigParser module contains ConfigParser class, Queue
module contains Queue class etc. Should I use CapWords for the
module (and file) name or not?

Since that's what it's called, you can't reasonably avoid it.
E.g. should I
from mess.wxmegaeidgets.GenericDialog import GenericDialog

or
from mess.wxmegaeidgets.genericdialog import GenericDialog

Have you tried either of these? Use the one that works.
(suppose that the module contains only the GenericDialog class. BTW,
will Py3K change the standard library names in order to follow PEP
8?)

There is a distinct "clean up the standard library" project; I can't
recall if it is intended to be part of Py3k.
Great, but what if I subclass wx widgets? They all have
CapWords. (wx.Window.ShowModal, wx.Window.Bind etc.) I must use
CapitalizedWords for method names because sometimes I have to override
inherited methods, and it is not possible to change their name. Where
should be the limit?

PEP 8 can't retroactively change the code you're importing. It's a
guideline for code that *you* write; i.e. when you write a new module,
or a new class or function, follow the guidelines.

Existing code is how it is, and a lot of code is already written that
relies on the names in e.g. wxWidgets. Only a backwards-imcompatible
upgrade (such as Py3k) can hope to remove those non-conformant names.
If I create a composite widget then should I use
lowercase and underscores?

If it's a class, use TitleCase; if it's an instance or a function, use
lower_case. If it *must* be the same name as something else
(e.g. you're overriding an existing inherited function), you have no
choice but to name it whatever the original is named.
If I create a class that is not itself a widget, but works with
widgets? The PEP defines the "theoretically good" coding style, but
what is the correct style in an environment where you have third
party libraries that are not following the PEP?

Follow PEP 8 for your code. Grit your teeth and use the non-standard
conventions of third-party libraries, until such time as they are
(backward-incompatibly) changed to be compliant.
 

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

Forum statistics

Threads
473,769
Messages
2,569,579
Members
45,053
Latest member
BrodieSola

Latest Threads

Top