Re: OO conventions
- From: bruno at modulix <onurb@xxxxxxxxxxx>
- Date: Fri, 03 Feb 2006 15:22:21 +0100
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@xxxxxxxxxxx'.split('@')])"
.
- Follow-Ups:
- Re: OO conventions
- From: Blair P. Houghton
- Re: OO conventions
- References:
- OO conventions
- From: Daniel Nogradi
- Re: OO conventions
- From: Claudio Grondi
- Re: OO conventions
- From: Blair P. Houghton
- OO conventions
- Prev by Date: Re: HTMLDocument and Xpath
- Next by Date: web.py + cheetah delivering incomplete pages
- Previous by thread: Re: OO conventions
- Next by thread: Re: OO conventions
- Index(es):
Relevant Pages
|