algebraic datatypes
Marcin 'Qrczak' Kowalczyk
qrczak at knm.org.pl
Sat Jun 23 10:21:03 EDT 2001
Sat, 23 Jun 2001 10:32:46 GMT, Ronald Legere <legere at adelphia.net> pisze:
> Essentially,
> datatype Tree = Node Tree Tree | Leaf Int
> should translate into a class structure like this:
from __future__ import nested_scopes
import new, types
def data(con, con_args):
if isinstance(con_args, types.StringType): con_args = (con_args,)
def __init__(self, *args, **kwargs):
i = 0
for arg in con_args:
if kwargs.has_key(arg):
self.__dict__[arg] = kwargs[arg]
del kwargs[arg]
else:
try:
self.__dict__[arg] = args[i]
except IndexError:
raise 'Too few arguments of %s provided' % con
i += 1
if i < len(args):
raise 'Too many arguments of %s provided' % con
if kwargs:
raise 'Unknown keyword arguments of %s' % con
globals()[con] = new.classobj(con, (), {'__init__': __init__})
# Perhaps it should be put in a different namespace?
# Example of usage:
data('Node', ('l', 'r'))
data('Leaf', ('anint'))
tree1 = Node(Leaf(1), Leaf(2))
tree2 = Node(l = Leaf(3), r = Leaf(4))
if isinstance(tree1, Node) and isinstance(tree1.r, Leaf):
print tree1.r.anint
# Types of fields, as well as the type of the tree itself,
# are implicit in Python.
# Exceptions should be better; I'm not familiar with existing
# conventions.
--
__("< Marcin Kowalczyk * qrczak at knm.org.pl http://qrczak.ids.net.pl/
\__/
^^ SYGNATURA ZASTĘPCZA
QRCZAK
More information about the Python-list
mailing list