tags:

views:

450

answers:

2

How does python handle object oriented constructs such as abstract, virtual, pure virtual etc

Examples and links would really be good.

+21  A: 

An abstract method is one that (in the base class) raises NotImplementedError.

An abstract class, like in C++, is any class that has one or more abstract methods.

All methods in Python are virtual (i.e., all can be overridden by subclasses).

A "pure virtual" method would presumably be the same thing as an abstract one.

In each case you could attempt deep black magic to fight against the language, but it would be (generally speaking) exceedingly silly to do so.

I've striven to deal with the "etc" part in two books, a dozen videos, two dozen essays and PDFs and other presentations, and I can't spend the next few days summarizing it all here. Ask specific questions, and I'll be glad to try and answer!

Alex Martelli
The Martellibot limited to "etc" and 4 lines ...
gimel
I did no such thing even when Steve Holden granted me that title 9+ years ago, see http://www.amk.ca/quotations/python-quotes/page-8 -- "The Martellibot Mark 1 has a completely European flavour to it, and adds a cosmopolitan touch of linguistics to its output, sprinkling foreign language references in. It is similar to the timbot in its overall erudition, but can be distinguished from it by its tendency to indulge in flamewars (which, I believe, it does mostly to convince us it is human). [[Steve Holden, 13 Dec 2000]]".
Alex Martelli
Anyway, a toast to passing the 10k mark on Stack Overflow.
gimel
Not yet -- 9997 as I write (was 9999 then got a downvote, gonna look into that now).
Alex Martelli
There, hopefully I helped push you over the edge! (Up the slope?)
Van Gale
As did I, well deserved :)
Dominic Rodger
+6  A: 

"How does python handle object oriented constructs such as abstract, virtual, pure virtual etc."

These are language constructs more than OO constructs. One can argue that abstract is a language-agnostic concept (even though Python doesn't need it.) Virtual and Pure Virtual are implementation details for C++.

There are two OO constructs that aren't necessary in Python but sometimes helpful.

The notion of "Interface" makes sense when (1) you have single inheritance and (2) you have static type-checking. Since Python has multiple inheritance and no static type checking, the concept is almost irrelevant.

You can, however, define "interface"-like superclasses which don't actually do anything except define the interface. It's handy for documentation. One idiom is the following.

class InterfaceMixin( object ):
    def requiredMethod( self ): raise NotImplemntedError()

class RealClass( SuperClass, InterfaceMixin ):
    def requiredMethod( self ):
         # actual implementation.

The notion of "Abstract" only makes sense when you have static type checking and you need to alert the compiler that there's no body in one or more methods in this class definition. It also alerts the compiler that you can't create instances. You don't need this in Python because the methods are located dynamically at run-time. Attempting to use an undefined method is just an AttributeError.

The closest you can do this kind of thing.

class AbstractSuperclass( object ):
    def abstractMethod( self ):
        raise NotImplementedError()

It isn't completely like Java or C++ abstract. It's a class with a method that raises an error. But it behaves enough like an abstract class to be useful.

To match Java, you'd have to prevent creating instances. This requires you to override __new__. If you did this, your concrete subclasses would then need to implement __new__, which is a pain in the neck, so we rarely take active steps to prevent creating instances of something that's supposed to be abstract.

The concept of "virtual" and "pure virtual" are C++ optimizations that force a method lookup. Python always does this.


Edit

Example of Abstract without the explicit method definition.

>>> class Foo( object ):
...     pass
... 
>>> f= Foo()
>>> f.bar()
Traceback (most recent call last):
  File "<stdin>", line 1, in <module>
AttributeError: 'Foo' object has no attribute 'bar'
S.Lott
in your first example the wrong exception is raised: a TypoError.
yairchu