Deferred Evaluation in Recursive Expressions?
Peter Otten
__peter__ at web.de
Tue May 9 12:06:42 EDT 2006
birchb at ozemail.com.au wrote:
> How about mutual recursion?
>
> class LinkedListA(TypeDef):
> typedef = (int, LinkedListB)
>
> class LinkedListB(TypeDef):
> typedef = (int, LinkedListA)
class Names(object):
def __getattribute__(self, name):
return name
types = Names()
class Type(type):
all = {}
def __new__(mcl, name, bases, dict):
assert name not in mcl.all, "name clash"
assert "_typedef" not in dict
dict["_typedef"] = dict.pop("typedef", ())
cls = type.__new__(mcl, name, bases, dict)
mcl.all[name] = cls
return cls
def get_typedef(cls):
get = cls.all.get
return tuple(get(item, item) for item in cls._typedef)
def set_typedef(cls, value):
cls._typedef = value
typedef = property(get_typedef, set_typedef)
class TypeDef:
__metaclass__ = Type
class LinkedListA(TypeDef):
typedef = (int, types.LinkedListB)
class LinkedListB(TypeDef):
typedef = (int, types.LinkedListA)
print LinkedListA.typedef
print LinkedListB.typedef
I'm sure it will break down somewhere :-)
Peter
More information about the Python-list
mailing list