OO conventions
bruno at modulix
onurb at xiludom.gro
Fri Feb 3 09:22:21 EST 2006
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 the superclass type.
Why "via the superclass type" ? "returns the object" is enough.
> The caller can then either check a flag in the superclass to see what
> type the subclass is,
Why the h... ? We don't care what type it is, as long at it does what we
expect it to do.
> or just assume it's the right type of image
Yes
(snip)
> (or does Python have RTTI?
Much better than 'RTTI'.
> I don't recall if
> I've seen it, yet...).
obj.__class__ is a reference to the class (which is itself an object...)
> Though if the filename doesn't match the content, someone should raise
> an exception...
Why ? filenames and extensions are nothing more than conventions.
Image.open() 's responsability is to create an object of the appropriate
class, period. If *your program* needs to ensure that the image type
matches the filename, it's your problem to check this.
> But this means that Image.open(filename) is a static method of the
> superclass, not requiring instantiation.
AFAIK, it's just a function in the Image module (which is itself an
object - instance of class module - but that's orthogonal).
> Image(string) could easily
> default to assuming string is a filename, doing the same work as
> Image.open(filename), though it would at least partially construct an
> Image instance each time it's called, which isn't what you want.
> Image.open(filename) defined as a static method (wait...is that
> possible in Python? I hate being the newbie)
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)
--
bruno desthuilliers
python -c "print '@'.join(['.'.join([w[::-1] for w in p.split('.')]) for
p in 'onurb at xiludom.gro'.split('@')])"
More information about the Python-list
mailing list