ten small Python programs

Paul McGuire ptmcg at austin.rr.com
Sat May 26 21:23:39 EDT 2007


I ***love*** this "10 Little Programs" idea!  As soon as I get a
breathing space, I'm going to add a "10 Little Parsers" page to the
pyparsing wiki!

On May 26, 2:38 pm, Steven Bethard <steven.beth... at gmail.com> wrote:
> <nitpick>
> Though the code should probably follow PEP 8 guidelines, e.g.
> under_scores instead of camelCase for object and method names:
>
>      http://www.python.org/dev/peps/pep-0008/
> </nitpick>

Really?  Underscore-separated words preferred over camel case?  What
is the rationale for this?  This style is so retro/80's/C-ish.  It
seems more like a Java backlash to me than anything else.  If we (or
Guido) don't like changing case to indicate word breaks, why are class
names to be UpperCamelCase, and not Capitalized_with_underscores?  If
there is a casing convention nit to pick, I'd focus on UpperCamelCase
for class names, lower case (either with underscores or mixed case)
for attributes and method names, and UNDERSCORE_SEPARATED_ALL_CAPS for
constants.

If we want to just say "well, PEP-8 says such and such," I think this
is an area where the thinking has possibly evolved since 2001.  Also,
I think the PEP would benefit from explicitly discouraging some
practices, such as Hungarian notation.

>
> >     class ShoppingCart:
> >         def __init__(self): self.items = []
> >         def buy(self, item): self.items.append(item)
> >         def boughtItems(self): return self.items
> >     myCart = ShoppingCart()
> >     myCart.buy('apple')
> >     myCart.buy('banana')
> >     print myCart.boughtItems()

If you want to nitpick, I'd rather go after the one-liner methods with
the body on the same line as the def statement.

How's this for a better non-trivial method example:

    MAX_ITEMS_FOR_EXPRESS_LANE = 10
    def canUseExpressLane(self):
        return (len(self.items) <= MAX_ITEMS_FOR_EXPRESS_LANE)

or call it "can_use_express_lane" if you must.

I guess pyparsing with its mixedCase functions and attributes is
doomed for the Dunce Corner.  Too bad for BeautifulSoup, cElementTree,
and wxPython that are also at variance with this canon of Python
coding style. ("Modules should have short, all-lowercase names. ...
Python packages should also have short, all-lowercase names, although
the use of underscores is discouraged.")

-- Paul




More information about the Python-list mailing list