How does python handle object oriented constructs such as abstract, virtual, pure virtual etc
Examples and links would really be good.
How does python handle object oriented constructs such as abstract, virtual, pure virtual etc
Examples and links would really be good.
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!
"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'