B
Bruno Desthuilliers
Matthias Kaeppler a écrit :
I beg your pardon ???
To specialize it (subtyping), or to add functionnalities (code reuse,
factoring)
I don't know of any language that provide such a thing. At least for my
definition of "correctly".
Could you share your definition of polymorphism ?
class Base(object):
def foo(self):
raise NotImplementedError, "please implement foo()"
Yes. inheritence is initially made for code sharing (cf Smalltalk). The
use of inheritence for subtyping comes from restrictions of statically
typed languages [1]. BTW, you'll notice that the GoF (which is still one
of the best references about OO) strongly advise to program to
interfaces, not to implementations. And you'll notice that some patterns
only exists as workarounds for the restrictions enforced by statically
typed languages.
[1] should say : for *a certain class of* statically typed languages.
There are also languages like OCaml that relies on type inference.
In OO, polymorphism is the ability for objects of different classes to
answer the same message. It doesn't imply that these objects should
inherit from a common base class. Statically typed languages like C++ or
Java *restrict* polymorphism.
(snip)
You don't need any of this.
class Foo:
def walk(self):
print "%s walk" % self.__class__.__name__
class Bar:
def walk(self):
print "I'm singing in the rain"
def letsgoforawalk(walker):
walker.walk()
f = Foo()
b = Bar()
letsgoforawalk(f)
letsgoforawalk(b)
Here, the function (BTW, did you know that in Python, functions are
objects too ?) letsgoforawalk expect an object that has the type 'object
that understand the message walk()'. Any object of this type will do -
no need to have a common base class.
Of course. Why should polymorphism need anything more ?
It is, once you've unlearned C++/Java/ADA/whatever.
Hi,
sorry for my ignorance, but after reading the Python tutorial on
python.org, I'm sort of, well surprised about the lack of OOP
capabilities in python.
I beg your pardon ???
Honestly, I don't even see the point at all of
how OO actually works in Python.
For one, is there any good reason why I should ever inherit from a
class?
To specialize it (subtyping), or to add functionnalities (code reuse,
factoring)
^^ There is no functionality to check if a subclass correctly
implements an inherited interface
I don't know of any language that provide such a thing. At least for my
definition of "correctly".
and polymorphism seems to be missing
in Python as well.
Could you share your definition of polymorphism ?
I kind of can't imagine in which circumstances
inheritance in Python helps. For example:
class Base:
def foo(self): # I'd like to say that children must implement foo
pass
class Base(object):
def foo(self):
raise NotImplementedError, "please implement foo()"
class Child(Base):
pass # works
Does inheritance in Python boil down to a mere code sharing?
Yes. inheritence is initially made for code sharing (cf Smalltalk). The
use of inheritence for subtyping comes from restrictions of statically
typed languages [1]. BTW, you'll notice that the GoF (which is still one
of the best references about OO) strongly advise to program to
interfaces, not to implementations. And you'll notice that some patterns
only exists as workarounds for the restrictions enforced by statically
typed languages.
[1] should say : for *a certain class of* statically typed languages.
There are also languages like OCaml that relies on type inference.
And how do I formulate polymorphism in Python?
In OO, polymorphism is the ability for objects of different classes to
answer the same message. It doesn't imply that these objects should
inherit from a common base class. Statically typed languages like C++ or
Java *restrict* polymorphism.
Example:
(snip)
You don't need any of this.
class Foo:
def walk(self):
print "%s walk" % self.__class__.__name__
class Bar:
def walk(self):
print "I'm singing in the rain"
def letsgoforawalk(walker):
walker.walk()
f = Foo()
b = Bar()
letsgoforawalk(f)
letsgoforawalk(b)
Here, the function (BTW, did you know that in Python, functions are
objects too ?) letsgoforawalk expect an object that has the type 'object
that understand the message walk()'. Any object of this type will do -
no need to have a common base class.
I could as well leave the whole inheritance stuff out and the program
would still work (?).
Of course. Why should polymorphism need anything more ?
Please give me hope that Python is still worth learning :-/
It is, once you've unlearned C++/Java/ADA/whatever.