polymorphism (was Re: Type checking in python?)

Richard Brodie R.Brodie at rl.ac.uk
Wed Jul 26 11:54:30 EDT 2000


"Oivvio Polite" <oivvio at cajal.mbb.ki.se> wrote in message news:8lmo4i$s28$1 at news.kth.se...

> For instance I want the constructor to take either a filename, a file object
> or a database connection object.

><paul at prescod.net> wrote:
>> Let me also point out that in many cases, this style of programming
>> would be frowned upon by serious Python programmers.

Polymorphism and method overloading aren't really the same thing at all.
What Paul was saying was that Python lacks (or doesn't want) a Java
like interface concept. So if you wanted a method to be able to write
to any stream, you can't specify that in the code. And attempting to
enumerate all possible streams would break polymorphism. Python
takes the approach that if it looks like a fish and smells like a fish, it
probably is a fish.

Now what you are doing is rather different. You have three
different things: there is nothing that you could say 'filename', 'file
object', and 'database connection' are all kinds of. Except 'object'
perhaps.

How to solve the problem? One approach would be to use a factory
style and have three functions: generateXFromFileName(), generateXFromFileObject(),
generateXFromDBConn().





More information about the Python-list mailing list