Le 2 juil. 2010 à 10:21, Yuri a écrit : > Hi! > > I would like to add some method to an existing content type class. > > What I've done now is using schemaextender and a custom marker > interface to a Document: > > class DocumentExtender(object): > """Adapter that adds a Ripped Off From Channel field to News Items.""" > adapts(IDocumentExtended) > implements(IOrderableSchemaExtender) > > _fields = [ > > Schemaextender can add new fields, I would like now to add some new > methods. From my understanding of ZCA, I've to write some adapter for > the custom interface. Is it correct? Is there some code/product about > this issue I can read from?
An adapter does not add any method to an existing class. Adapters work on objects and not on classes. See the overview of the adapter pattern: http://www.muthukadan.net/docs/zca.html#the-adapter-pattern Adding a method to an existing class is just as easy as hereafter and works in any Python app: >>> # Original class >>> class Foo(object): ... pass ... >>> # Additional method >>> def bar(self, value): ... return value * 2 ... >>> setattr(Foo, 'bar', bar) >>> f = Foo() >>> f.bar(2) 4 If you want to do it in a "clean and documented" way, have a look at collective.monkeypatcher at Pypi. HTH -- Gilles > > Thank you very much for any pointer. > _______________________________________________ > Product-Developers mailing list > [email protected] > http://lists.plone.org/mailman/listinfo/product-developers _______________________________________________ Product-Developers mailing list [email protected] http://lists.plone.org/mailman/listinfo/product-developers
