#1
  1. No Profile Picture
    Registered User
    Devshed Newbie (0 - 499 posts)

    Join Date
    Sep 2003
    Posts
    11
    Rep Power
    0

    Question Does interface-only inheritance make sense in Python ?


    In statically typed languages like Java & C++ you _need_ to use inheritance to accomplish polymorphism, so people have grown accustomed to doing this. But in python you can have polymorphism whenever 2 objects have a certain method in common, regardless of their type. Yet I often encounter things like this:

    Code:
    class Shape:
        def draw(self): pass
    
    class Circle(Shape):
        def draw(self):
            print "Drawing Circle"
    
    class Rectangle(Shape):
        def draw(self):
            print "Drawing Rectangle"
    This doesn't make any sense to me. Circle & Rectangle can be substituted polymorphically without inheriting from Shape, so I think it is unnecessary to clutter your code with practices from statically typed languages.

    Python offers both interface and implementation inheritance at the same time, something that should be used appropriately (When you need to inherit the interface AND the implementation).

    Anyway, that's what I think, feel free to prove me wrong
    Last edited by Cuboidz; September 10th, 2003 at 12:27 PM.
  2. #2
  3. No Profile Picture
    Contributing User
    Devshed Newbie (0 - 499 posts)

    Join Date
    Dec 2001
    Location
    Houston, TX
    Posts
    383
    Rep Power
    14
    If you aren't inheriting actual methods from the parent class, I find that using the same conventions in Python is unnecessary. If you really want, you can do it, but it's ugly. If you feel the need to share a common parent in that case, just make it an empty class.

    Code:
    class Shape: pass
    This is often useful for exceptions, I find, but not much else.

IMN logo majestic logo threadwatch logo seochat tools logo