#1
  1. Contributing User
    Devshed Novice (500 - 999 posts)

    Join Date
    Feb 2005
    Posts
    611
    Rep Power
    65

    Red face Class Inheritance


    What is the telltale indicating that one class inherited from another class?
  2. #2
  3. No Profile Picture
    Contributing User
    Devshed Novice (500 - 999 posts)

    Join Date
    Nov 2004
    Location
    There where the rabbits jump
    Posts
    556
    Rep Power
    11
    Code:
    >>> class C1:
    	def Hello(self):
    		print "Hello"
    
    		
    >>> class C2(C1):
    	def Bye(self):
    		self.Hello()
    		print "Bye"
    >>> c1 = C1()
    >>> c2 = C2()
    >>> c1.Hello()
    Hello
    >>> c2.Bye()
    Hello
    Bye
    >>>
    This is a bad example but anything that is in the first class or above the one that is inheriting can be used by the second below it
    Last edited by monkeyman23555; June 11th, 2005 at 10:33 AM.
    Those people who think they know everything are a great annoyance to those of us who do.
  4. #3
  5. No Profile Picture
    Contributing User
    Devshed Intermediate (1500 - 1999 posts)

    Join Date
    Feb 2004
    Location
    London, England
    Posts
    1,585
    Rep Power
    1373
    From the docs:
    issubclass(class, classinfo)
    Return true if class is a subclass (direct or indirect) of classinfo. A class is considered a subclass of itself. classinfo may be a tuple of class objects, in which case every entry in classinfo will be checked. In any other case, a TypeError exception is raised. Changed in version 2.3: Support for a tuple of type information was added.
    There is also an isinstance function to test if an object is an instance of a class or one of its subclasses.

    Dave - The Developers' Coach
  6. #4
  7. No Profile Picture
    Contributing User
    Devshed Newbie (0 - 499 posts)

    Join Date
    Jun 2004
    Posts
    461
    Rep Power
    25
    If you are wondering when to tell if a class should inherant from anouther class, I was always taught only do it if it can pass the "is a" or "is an" test

    So like:
    apple is a fruit
    dog is a animal

    but things like this would be considered bad:
    bird is a dog
    grape is a apple

    because everyone knows that there is no dog bird, only bird dogs..

    That is just a nice easy way to tell, Thats just about the most I got out of java.

    Comments on this post

    • netytan agrees
  8. #5
  9. No Profile Picture
    Contributing User
    Devshed Newbie (0 - 499 posts)

    Join Date
    Feb 2005
    Posts
    78
    Rep Power
    10
    in addition to isinstance and issubclass there's also type(). You can also use obj.__class__ and cls.__bases__:
    Code:
    class Foo(object): pass
    class Bar(object): pass
    class Baz(Foo, Bar): pass
    x = Baz()
    
    print x.__class__
    for base in x.__class__.__bases__:
        print base, base.__bases__
    Originally Posted by CyBerHigh
    If you are wondering when to tell if a class should inherant from anouther class, I was always taught only do it if it can pass the "is a" or "is an" test
    the "is a(n)" test can be good for getting the basic structure down, but the problem is that it makes you thing in terms of nouns - Car is a Vehicle.

    The standard use of OO in Python is known as "duck typing" because you don't care about what class an object is - if it walks like a duck and quacks like a duck it can be treated like a duck - ie: Objects which provide a particular function can be treated alike for the purpose of calling that function, even if everything else about that object is different:
    Code:
    class Duck(str):
        def foo(self): pass
        def quack(self):
            return "Duck quacking"
    
    class Doctor(list):
        def bar(self): pass
        def quack(self):
            return "Doctor faking it"
    
    x = Duck("donald")
    y = Doctor("Who")
    for elem in (x, y):
        print elem, "|", elem.quack()
    So rather than thinking of "is a", you think of things in terms of common capabilities - the "can <action>" test, eg:
    * Button is Selectable, Clickable, etc
    * Goblin is Attackable, Converseable, etc.
    * Car is Driveable, Crashable, etc
    This makes you think in terms of verbs - ie: your interface.

    There's also the compositional "has a" test:
    * Window has a (TitleBar, MenuBar, DisplayPane, Border)
    Code:
    class TitleBar(Draggable)
        def __init__(self, parent):
            self.parent = parent
    
        def on_drag(self):
            parent.do_drag()
    
    class Window(object):
        def __init__(self):
            self.titlebar =TitleBar(self)
            #...etc...
    
        def do_drag(self):
            pass     # move all components
    --OH.

IMN logo majestic logo threadwatch logo seochat tools logo