I have been using Python for about a year now, coming from a mostly Java background. I found Python quite easy to learn because of its focus on readability and simple d开发者_如何学Goesign. The thing I don't understand about python is why for a language that focuses so heavily on readability, it often uses very non-descriptive names for modules, functions, constants etc.. One thing I like about Java is its very descriptive class/attribute/method names (I like objective-C even more for this reason). It seems python programmers in general seem to have taken a C type approach to naming where they use as short names as possible for everything. I know everyone wants to do as little typing as possible but I like a lot of programmers spend the majority of my time reading code rather that writing it so I find the choice between short non-descriptive names and long descriptive names, an easy one to make. (I like longer descriptive names xD)
A few examples, just looking at some modules in the standard library,
- sched — Event scheduler, Could this have been EventScheduler?
- asyncore — Asynchronous socket handler, AsynchronousSocketHandler?
- imghdr — Determine the type of an image, DetermineImageType?
- Pickle?
I know this isn't a huge issue but I find myself more often than not having to look up the meaning of any new (or forgotten) module I come across when in other languages like Objective-C or Java I can get this meaning straight away from the modules/functions/attributes definition. On another note, people tend to write code similar to the way the standard library is written so you can be sure that if the standard library uses non-descriptive names the average developer will use even more non-descriptive names.
I was just wondering does anyone know why this is?
I guess there's a balance to be struck between being descriptive and concise, and Python's scripting background makes it somewhat more concise than Java (because us hobbyists are too lazy for all that typing ;-) ). At the risk of sounding like a fanboy, Python tries to walk the line between the descriptive-but-long (Java), and the short-but-tricky (*cough*Perl).
Things that are used often and easily understood can be short, so we have a str
type rather than an AsciiString
or UnicodeString
(Python2/3 respectively). More specialised functions, like urllib.urlencode
or random.normalvariate
get longer names.
The core language is generally kept simple (e.g. there is no character type, only one-character strings). The idea that there's only "one right way to do it", along with duck typing, mean that there's no need for names like do_something_with_type_a
. And, while it's just an excuse, there is clear documentation for anything that's not obvious.
As for itertools? The module name doesn't really matter, it's just a grouping of functions. Some of the functions are clearer (chain
, cycle
, repeat
), some less so (islice
, izip
). I suppose we assume that concepts like "zipping" and "slicing" are straightforward once you're familiar with Python.
sched/asyncore/imghdr: All admittedly brief and undescriptive, but I've never seen any of them used. They probably date back to the days of 8-character filenames, and updating them has never been a priority.
pickle: Quirky, but you really only have to look it up once, then it's obvious. You couldn't really call it "serializer", because it's for a specific serialisation, not a generic framework.
Take a look at PEP 8
Highlights:
Modules should have short, all-lowercase names
Almost without exception, class names use the CapWords convention. Classes for internal use have a leading underscore in addition.
Function names should be lowercase, with words separated by underscores as necessary to improve readability.
And then there's the Zen of Python. In any case, AFAIK there is no prescription for using explicit, descriptive, obvious names driven by the Python community - it's left up to the developer.
精彩评论