pass object or use self.object?

Bruno Desthuilliers bruno.42.desthuilliers at websiteburo.invalid
Wed Apr 7 10:34:57 CEST 2010


Lie Ryan a écrit :
(snip)

> Since in function in python is a first-class object, you can instead do
> something like:
> 
> def process(document):
>     # note: document should encapsulate its own logic
>     document.do_one_thing()

Obvious case of encapsulation abuse here. Should a file object 
encapsulate all the csv parsing logic ? (and the html parsing, xml 
parsing, image manipulation etc...) ? Should a "model" object 
encapsulate the presentation logic ? I could go on for hours here...

> 
> and I think for your purpose, the mixin pattern could cleanly separate
> manipulation and document while still obeying object-oriented pattern
> that document is self-sufficient:
> 
> # language with only single-inheritance can only dream to do this
 >
> class Appendable(object):
>     def append(self, text):
>         self.text += text
> class Savable(object):
>     def save(self, fileobj):
>         fileobj.write(self.text)
> class Openable(object):
>     def open(self, fileobj):
>         self.text = fileobj.read()
> class Document(Appendable, Savable, Openable):
>     def __init__(self):
>         self.text = ''

Anyone having enough experience with Zope2 knows why this sucks big time.



More information about the Python-list mailing list