[Python-ideas] A "compound" object for Python like in ABC
Mark Janssen
dreamingforward at gmail.com
Wed Aug 17 10:10:56 CEST 2011
Just throwing out an idea here. I want to consider the programming
value of a Compound data type, such as found in the ABC programming
language.
I think it could considerably simplify Python in several different
ways although it would require some significant changes.
Crudely, a Compound is simply the ability to associate a label or key
with an object -- flatly; i.e. in a "planar" dimension rather than a
depth dimension like one might do with a variable name.
We can denote a Compound with a (key:value) syntax, using a colon to
signify the relationshiop.
Compounds may collide when they are put into a Set (if their keys are
the same). The default behavior can be like dict: overwrite the value
-- but users can subclass the Compound data type and specify what to
do. In the case of a Bag, it could __add__ the values; in the case of
a database, it could throw an exception, etc.
A Compound also has a __default__ value in the case of colliding with
a non-compound type. For a countable Compound this would likely be 1.
A dictionary becomes simply a set of Compounds; for example, {'a':1,
'b':2} is a *set* containing two Compound data elements.
Nicely, this allows the empty set to have the normal syntax of "{}".
Set would have to add the [] set/getitem syntax to deal with the
common case of possible compound relationships among its members. It
would return None if the item is not a Compound type otherwise it
returns the compound's value.
Additionally, set should NOT quietly ignore adding a duplicate
element, they should collide and python's pre-defined collision
behavior decides what happens.
Creating a bag (or Counter) container now becomes very simple: make a
Compound that run the add function for collisions.
As another example, a Node (in a graph) is now simply
"Node1":{"edge1", "edge2"} (a Compound) and a Graph simply a set of
such Compounds.
Some other possibilities: the Compound is a special data type that
exist in the border between an atom and a collection. The Compound's
constructor can exploit this. If a collection is passed as the first
paramater to the compound's constructor, all sorts of things can be
done. Compound(myset, 1) could return a dictionary with all values
set to 1. Given a list instead, it could return an enumeration.
This is all rather sloppy, but I wanted to put it out here to see what
kind of interest there might be....
Mark
More information about the Python-ideas
mailing list