3

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 design. 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?

toc777
  • 2,607
  • 2
  • 26
  • 37
  • 5
    I understand your point in general, but can you give some specific examples that give you this feeling? – moinudin Dec 20 '10 at 16:22
  • This is a subjective question, however I feel it's vital and a good one. Could you mark this as community wiki? – the_drow Dec 20 '10 at 16:24
  • should be on programmers.se; question submitters can't mark their subjective questions as community wiki anymore. – Wooble Dec 20 '10 at 16:26
  • I think its personal preference. But, many modules, methods have descriptive names. – dheerosaur Dec 20 '10 at 16:30
  • 1
    To be honest, I can only think of a few things that could use longer/more verbose names. Things like `itertools` may not sound "enterprisey", but they work and they sure beat names like `CollectionOfRandomIterators` ;) –  Dec 20 '10 at 16:34
  • @delnan I know I'll be flamed for this but would much prefer CollectionOfRandomIterators to itertools. – toc777 Dec 20 '10 at 16:48
  • Well, this topic *is* very subjective. In fact, I agree with sched, asyncore and imghdr (but truth to be told, I've never heard of them in the first place - they seem to be rarely-used) being opaque names. `CollectionOfRandomIterators` seems way to verbose though. –  Dec 20 '10 at 17:18
  • `itertools` is a module full of tools for creating and manipulating iterators. That seems appropriate to me. Better than `libxml` - I mean, what information is added to a module name by prepending `lib`? – Robert Rossney Dec 20 '10 at 19:31

2 Answers2

2

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.

Thomas K
  • 39,200
  • 7
  • 84
  • 86
  • This is a very good answer, I completely agree with things like str instead of XXXString but these should be limited to the absolute core and most used language utilities. I understand your point about pickle also but it encourages people to use obscure names like BeautifulSoup etc.. I like code that I read and understand without having to look up documentation, to me its a sign of great programming. – toc777 Dec 20 '10 at 18:20
  • @toc777: BeautifulSoup is not exactly self explanatory, I agree. But once you've imported the module and instantiated the class, most of what you actually use is sensibly named (`children`, `findAll`, etc.). And it's easy to search for ;-). I think it comes with growing up as a "fun" language rather than an enterprise thing. Python itself isn't named after a snake, after all. – Thomas K Dec 20 '10 at 18:33
0

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.

Community
  • 1
  • 1
Andrew Sledge
  • 10,163
  • 2
  • 29
  • 30