Reference class in class creation

Gregor Horvath gh at gregor-horvath.com
Tue Nov 21 16:11:50 CET 2006


Carl Banks schrieb:

> You could change it to look like this instead:
> 
> def __new__(metatype,name,bases,clsdict):
>     # do some weird stuff here
>     cls = type.__new__(metatype,name,bases,clsdict)
>     cls.a = SomeContainer(cls)
>     return cls

Cark, many thanks for your suggestion, it solved my problem.

I changed the metaclass:

class ControllerMeta(type):
    def __new__(meta, class_name, bases, new_attrs):
        cls = type.__new__(meta, class_name, bases, new_attrs)
        cls.__classinit__.im_func(cls, new_attrs)
        cls.save = validate(cls.widget_edit)(bases[0].save)
        return cls

and the Tour Class:

class Tour(MotourController):
  __metaclass__ = ControllerMeta
  sqlobjectclass = model.Tour

  def __classinit__(cls, newattrs):
      cls.widget_edit = TourEdit(controller=cls, name = "tourheader",
                                 fields = [Label(name="id")])


And this seems to work fine.

Thanks
--
Greg



More information about the Python-list mailing list