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 like > > image = Image( ) > image.read( "myfile.jpg" ) > image.rotate( ) > image.close( ) > > But now it turns out that the PIL module uses this as > > image = Image.open( "myfile.jpg" ) > image.verify( ) > image.rotate( ) > image.close( ) > > Perhaps the real Image class of PIL doesn't have these methods > exactly, but doesn't matter, my point is the way it works. Is it > normal that instead of first creating an instance of a class, it > starts right away with one its methods? I of course understand that > the PIL people simply made a choice that their module works this way, > period, but I'm just wondering if it wouldn't have been more "logical" > to do it along the way of my first example. > > I guess it's just a matter of convention or how the programmer feels > like, but there are no conventions of this type? Which would be more > pythonic? Or I shouldn't worry and it's totally up to the developer?
If I were coding a class like that, I'd probably do it like this: image = Image("myfile.jpg") image.rotate() image.close() And, in addition, there would be an image.open() method. In the above, __init__ just calls it on its own when the filename is specified. (Though, of course, I'm not at all familiar with the PIL module; there might be more stuff going on behind the scenes with the way they do it.) -Kirk McDonald -- http://mail.python.org/mailman/listinfo/python-list