returning unordered keyword arguments from a function (WAS: Are multiple return values really harmful?)

Hung Jung Lu hungjunglu at yahoo.com
Fri Nov 19 09:33:44 EST 2004


Steven Bethard <steven.bethard at gmail.com> wrote:
> Fernando Perez wrote:
> > Steven Bethard wrote:
> >>>>> r = object(year=2004, month=11, day=18)
> > Given that the necessary class is literally a 3-liner, ...
> Even at 3 lines, do you really want to rewrite those every time you need 
> this functionality?  

I have written the one-liner "class Generic: pass" all too many times.
:)

Generic objects can be used to represent hierarchical data in tree
fashion. As I said, generic objects are also good for
pickling/serialization. We are talking about communication between
multiple applications, across time and/or space. Other representations
include dictionaries or XML. But you can tell that when it comes to
compactness and ease of use, generic objects are the way to go. In
fact, most common hierarchical data structures only need: (1) the
generic object, (2) list, (3) basic types (numbers and strings.)

It seems odd that there is no standard generic object in Python.

Actually, if one thinks outside Python, in prototypish languages,
generic objects are more fundamental. They are the building block of
everything else. You don't build your 3-liner generic object from
something else... all on the contrary, you build everything else from
your generic object.

The syntax "object(year=2004, month=11, day=18)" certainly is nice. I
wouldn't be surprised that somewhere, someone has already written some
programming language that uses this syntax for their fundamental
generic object.

> On the other hand, I usually find that in the few places where I have 
> used a record like this, I eventually replace the struct with a real 
> class...

This is true for single programs. It's true for function arguments or
outputs. In those places, generic objects are good as the quick and
easy way of using hierarchical data structure without the need of
formally defining a class. Once things deserve to be replaced by real
classes, they are replaced.

This is not true for pickling/serialization purpose. When you have
pickled data, you don't want to have to search for the definition of
the classes, which you or someone else may have written years ago. You
want to be able to unpickle/unserialize your data and use it, without
the need of class definition. Yes, dictionary can be used, but you:

(a) either use mydata['a']['b']['c']['d'] instead of mydata.a.b.c.d,
or

(b) have a class to convert dictionary-based back to object-based
(hence we come back to the problem: where is that code file of the
class that some guy wrote 7 years ago?)

If I have avoided anything more complicated than "class Generic:
pass", it's because this is a one-liner that I can remember how to
type anytime. :) Now, if in the language there is something standard,
even this trick won't be necessary. From all what I can see, "class
Generic: pass" will stay as the preferred choice for many people, for
a long time to come. Simplicity counts.

Hung Jung



More information about the Python-list mailing list