Re: OO conventions

2006-02-06 Thread Nicola Musatti
Fredrik Lundh wrote: > Nicola Musatti wrote: [...] > > What is important to me is to keep your get_initial_data() function > > outside Klass if it's task is non trivial, e.g. it has to interact with > > the OS or a DB. > > why ? Separating the internal logic of an application from its interaction

Re: OO conventions

2006-02-06 Thread Scott David Daniels
Fredrik Lundh wrote: > Nicola Musatti wrote: >> What is important to me is to keep your get_initial_data() function >> outside Klass if it's task is non trivial, e.g. it has to interact with >> the OS or a DB. > > why ? In order to simplify testing. --Scott David Daniels [EMAIL PROTECTED] -- ht

Re: OO conventions

2006-02-06 Thread Daniel Nogradi
> > So after all, what is a 'factory' or 'factory function'? > > A brief explanation in Python terms is at > http://www.aleax.it/ep03_pydp.pdf -- "pages" (slides) 37-44 (the rest of > the presentation is about an even more fundamental design pattern, > "template method"). A far more extensive essa

Re: OO conventions

2006-02-06 Thread Fredrik Lundh
Nicola Musatti wrote: > > def factory(info): > > k = Klass() > > data = get_initial_data(info) > > k.set_data(data) > > return k > > > > to: > > > > def factory(info): > > data = get_initial_data(info) > > return Klass(data) > > > > What would be the value of doing the init

Re: OO conventions

2006-02-06 Thread Nicola Musatti
I V wrote: > Nicola Musatti wrote: [...] > > Factory functions (or classes) are there to solve this problem and > > still allow a clean separation of concerns. Although instances of Klass > > are created uninitialized, they only live in this state within their > > factory and only reach trhe outsi

Re: OO conventions

2006-02-05 Thread Blair P. Houghton
Alex Martelli wrote: > As you see, static methods have a small extra lookup cost (a couple > hundred nanoseconds on my oldish laptop); I would've expected the opposite effect...I guess the runtime considers instances more active than the static portion of a class. > "Premature > optimization is

Re: OO conventions

2006-02-04 Thread Alex Martelli
Blair P. Houghton <[EMAIL PROTECTED]> wrote: ... > > It is (search for 'staticmethod' and 'classmethod'). But there's not > > much use for 'static methods' in Python - we usually just use plain > > functions ('classmethods' are another beast - much more useful than > > staticmethods) > > Does i

Re: OO conventions

2006-02-04 Thread Alex Martelli
Daniel Nogradi <[EMAIL PROTECTED]> wrote: ... > So after all, what is a 'factory' or 'factory function'? A brief explanation in Python terms is at http://www.aleax.it/ep03_pydp.pdf -- "pages" (slides) 37-44 (the rest of the presentation is about an even more fundamental design pattern, "templat

Re: OO conventions

2006-02-04 Thread Steve Holden
Daniel Nogradi wrote: [...] > So after all, what is a 'factory' or 'factory function'? The name is intended to be indicative: it's a function that makes things - usually instances of some class. As has already been pointed out. Image is a module from PIL, so Image.open() is a function in that m

Re: OO conventions

2006-02-04 Thread Daniel Nogradi
> > Actually, this way of creating a class instance is good OO practice in > > many places: The Image.open() method acts as a factory-function for > > creating Image objects. > > You don't know, until you inspect the return value, if the created > > object is actually an instance of class Image or

Re: OO conventions

2006-02-03 Thread Blair P. Houghton
bruno at modulix wrote: > Blair P. Houghton wrote: > > So Image.open(filename) seems right as a factory function that opens > > the file, figures out what it really is, constructs the appropriate > > subclass object (likely by passing a string to the constructor, e.g., > > JPGImage(filename)), and

Re: OO conventions

2006-02-03 Thread I V
Nicola Musatti wrote: > I don't think this is all there is to it. Even though a class such as > Image might not have a sensible default, initial state it still might > not be reasonable to burden it with the ability to collect the > information needed to reach such an initial state. To put it it an

Re: OO conventions

2006-02-03 Thread bruno at modulix
Blair P. Houghton wrote: (snip) > So Image.open(filename) seems right as a factory function that opens > the file, figures out what it really is, constructs the appropriate > subclass object (likely by passing a string to the constructor, e.g., > JPGImage(filename)), and returns the object via th

Re: OO conventions

2006-02-03 Thread Nicola Musatti
Steven D'Aprano wrote: [...] > If a class has a natural, obvious default state (e.g. a mutable string > class might start off empty, a mutable int class might start off as zero, > a binary tree might start off as an empty node with no children) then it > makes sense to initialise the class, then a

Re: OO conventions

2006-02-03 Thread Steven D'Aprano
On Thu, 02 Feb 2006 17:00:26 -0800, Blair P. Houghton wrote: > Image would be a superclass to JPGImage, BMPImage, PNGImage, etc... > > But which to use could only be determined AFTER opening the file, > because "file.jpg" doesn't have type JPG, it has type string and > semantic value "maybe a jpe

Re: OO conventions

2006-02-02 Thread Blair P. Houghton
Image would be a superclass to JPGImage, BMPImage, PNGImage, etc... But which to use could only be determined AFTER opening the file, because "file.jpg" doesn't have type JPG, it has type string and semantic value "maybe a jpeg file or maybe something misnamed as a jpeg file". So Image.open(filen

Re: OO conventions

2006-02-02 Thread Daniel Nogradi
> > > In this case, Image seems to be a python module, with the open function > > > defined, PIL's Image is not a class. > > > > > > > Thanks for the enlightening remarks, especially this last one, indeed, > > it's not a class. > > Actually, this way of creating a class instance is good OO practice

Re: OO conventions

2006-02-02 Thread Alex Martelli
Magnus Lycka <[EMAIL PROTECTED]> wrote: > Alex Martelli wrote: > > Steven D'Aprano <[EMAIL PROTECTED]> wrote: > >>And now, at long last, the image object actually is an image. So why make > >>this a two step process? Whatever the Image() initialization does, why > >>can't it be done automatically

Re: OO conventions

2006-02-02 Thread Magnus Lycka
Alex Martelli wrote: > Steven D'Aprano <[EMAIL PROTECTED]> wrote: >>And now, at long last, the image object actually is an image. So why make >>this a two step process? Whatever the Image() initialization does, why >>can't it be done automatically when you read the file? > > "Two-step construct" (

Re: OO conventions

2006-02-02 Thread Alex Martelli
Steven D'Aprano <[EMAIL PROTECTED]> wrote: > > image = Image( ) > > Now you have an "image" object. What is it? > > Answer: it isn't an image at all, not in the plain English sense. (Or if > it is, it is an arbitrary "default image" picked by the class designer.) No doubt (presumably some kind o

Re: OO conventions

2006-02-02 Thread Tim N. van der Leeuw
Daniel Nogradi wrote: > > > > In this case, Image seems to be a python module, with the open function > > defined, PIL's Image is not a class. > > > > Thanks for the enlightening remarks, especially this last one, indeed, > it's not a class. Actually, this way of creating a class instance is good

Re: OO conventions

2006-02-02 Thread Steven D'Aprano
On Wed, 01 Feb 2006 23:40:37 +0100, Daniel Nogradi wrote: > I'm relatively new to object oriented programming, so get confused > about its usage once in a while. Suppose there is a class Image that > has a number of methods, rotate, open, verify, read, close, etc. Then > to use this class my natur

Re: OO conventions

2006-02-01 Thread Fredrik Lundh
Terry Hancock wrote: > What Lundh is modeling this on is not standard OOP thinking, > but rather the standard way of opening files in Python. > > Or you could say he's using a "factory function". "open" is a factory function, and it picks the right class based on the file you're opening. anyone

Re: OO conventions

2006-02-01 Thread Terry Hancock
On Wed, 1 Feb 2006 23:40:37 +0100 Daniel Nogradi <[EMAIL PROTECTED]> wrote: > I'm relatively new to object oriented programming, so get > confused about its usage once in a while. Suppose there is > a class Image that has a number of methods, rotate, open, > verify, read, close, etc. Then to use th

Re: OO conventions

2006-02-01 Thread Daniel Nogradi
> > In this case, Image seems to be a python module, with the open function > defined, PIL's Image is not a class. > Thanks for the enlightening remarks, especially this last one, indeed, it's not a class. -- http://mail.python.org/mailman/listinfo/python-list

Re: OO conventions

2006-02-01 Thread Kirk McDonald
Daniel Nogradi wrote: > I'm relatively new to object oriented programming, so get confused > about its usage once in a while. Suppose there is a class Image that > has a number of methods, rotate, open, verify, read, close, etc. Then > to use this class my natural guess would be to have something l

Re: OO conventions

2006-02-01 Thread Claudio Grondi
Daniel Nogradi wrote: > I'm relatively new to object oriented programming, so get confused > about its usage once in a while. Suppose there is a class Image that > has a number of methods, rotate, open, verify, read, close, etc. Then > to use this class my natural guess would be to have something l

Re: OO conventions

2006-02-01 Thread Anthony Greene
On Wed, 01 Feb 2006 23:40:37 +0100, Daniel Nogradi wrote: > I'm relatively new to object oriented programming, so get confused > about its usage once in a while. Suppose there is a class Image that > has a number of methods, rotate, open, verify, read, close, etc. Then > to use this class my natur

Re: OO conventions

2006-02-01 Thread Anthony Greene
On Wed, 01 Feb 2006 23:40:37 +0100, Daniel Nogradi wrote: > I'm relatively new to object oriented programming, so get confused > about its usage once in a while. Suppose there is a class Image that > has a number of methods, rotate, open, verify, read, close, etc. Then > to use this class my natur