Re: OO conventions



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('@')])"
.



Relevant Pages

  • Re: Can i pass a SqlConnection object with opened connection to a method/function as a parameter?
    ... methods in your long concatenated string, ... DataTools.EscapeMask method (a static method, apparently), or the ... > connection and what you do with it when you get it ... >> the existed opened sqlconnection object to do it and pass the object to ...
    (microsoft.public.dotnet.framework)
  • Re: Accessing functions defined in Global.asax
    ... Anjali Lourda wrote: ... > public function getNameas String ... better off creating a class with a static method and referencing the ... Ryan Walberg, B.Sc., MCSD, MCSD for .NET ...
    (microsoft.public.dotnet.framework.aspnet)
  • Re: Java syntax extension proposal
    ... You can already reference a static method through an instance, but most sources I've read regard that as a bad practice. ... It allows a static method from one class be called on an instance variable of another class (Let's say String). ...
    (comp.lang.java.advocacy)
  • Re: Inheritable Static methods
    ... Any static method, ... classname to a string, without hardcoding a string. ... encapsulate it into the class itself, so that instead of having the ... the former encapsulates the conversion into the int type. ...
    (microsoft.public.dotnet.languages.csharp)
  • Re: Static vs. instance method: Which one performs better?
    ... yield any significant result. ... In theory a static method might be very, very, *very* slightly faster ...
    (microsoft.public.dotnet.languages.csharp)