polymorphism

Erik Max Francis max at alcyone.com
Mon May 12 22:40:31 EDT 2003


Haris Bogdanovic wrote:

> How is polymorphism implemented in Python ?
> Is there a substitution for C++ virtual functions ?
> Can you give me a short example of how this works ?

In Python, _every_ method is "virtual":

>>> class C:
...  def f(self):
...   print "I'm C.f"
... 
>>> class D(C):
...  def f(self):
...   print "I'm D.f"
... 
>>> c = C()
>>> c.f()
I'm C.f
>>> d = D()
>>> d.f()
I'm D.f

-- 
 Erik Max Francis / max at alcyone.com / http://www.alcyone.com/max/
 __ San Jose, CA, USA / 37 20 N 121 53 W / &tSftDotIotE
/  \ I'm the woman whose / Three wishes came true
\__/ Lamya
    Fauxident / http://www.alcyone.com/pyos/fauxident/
 A "faux" ident daemon in Python.




More information about the Python-list mailing list