naming convention for scalars, lists, dictionaries ...

Paul Boddie paul at boddie.org.uk
Tue Mar 1 05:11:43 EST 2005


beliavsky at aol.com wrote in message news:<1109613115.760962.292360 at z14g2000cwz.googlegroups.com>...
> Since Python does not have declarations, I wonder if people think it is
> good to name function arguments according to the type of data structure
> expected, with names like "xlist" or "xdict".

Your suggestion coincides partly with a mechanism I developed recently
for my libxml2dom package. The normal libxml2dom package puts a
DOM-style wrapper around the existing Python wrapper objects -
something that Python's dynamic features accomplish easily - but this
incurs a major performance cost. Given that a low-level API
(libxml2mod) exists and provides a means to exchange fairly simple
and/or opaque objects with the library, I wondered if I couldn't just
write a code transformer which takes DOM-like code and emits code to
use the low-level API. For example:

  element.childNodes -> libxml2mod.children(element)

The challenge, as you've noted with your mention of declarations, is
to find out whether a particular name refers to an object of a
suitable type for the kind of transformations I have in mind.
(Alternatively, one could just guess that "childNodes" is a DOM-style
attribute and do the transformation, possibly leading to mistaken
transformations taking place.) Whilst type inference might offer a
solution, it is itself a much bigger project than this, so my quick
solution was to permit the annotation of names using prefixes to
indicate which names refer to DOM-style objects. For example:

  # Special magic defined earlier says that x2_ is the chosen prefix.
  x2_element.childNodes -> libxml2mod.children(x2_element)

The result of this is libxml2macro [1], an experimental interface to
libxml2 which manages to retain much of the space/time performance of
that library, albeit without addressing the divisive issues of
transparent memory management. It also offers an insight into the
"optional static typing" parallel universe for Python...

Paul

[1] http://www.boddie.org.uk/python/libxml2dom.html



More information about the Python-list mailing list