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

    Join Date
    Feb 2013
    Posts
    100
    Rep Power
    2

    Type vs class in Python?


    I've browsed around a lot on the internet, but still I am not able to figure out the difference between the two. (Or they are the same things?)
    May someone please explain it to me in simple terms?
    Thanks.
    Also, what is the difference between is-a and has-a relationship in Python?
    Last edited by Akshat1; June 18th, 2013 at 02:32 PM.
  2. #2
  3. No Profile Picture
    Contributing User
    Devshed Newbie (0 - 499 posts)

    Join Date
    Dec 2012
    Posts
    114
    Rep Power
    3
    From Python 2.3 forward, a ("new-style") class and a type are the same thing.

    In many other object-oriented languages they are not. In these languages a class is generally a kind of type, but there are "primitive" types which are not classes. These typically don't have methods, can't be subclassed, and their instances are not considered instances of Object.
  4. #3
  5. Banned ;)
    Devshed Supreme Being (6500+ posts)

    Join Date
    Nov 2001
    Location
    Woodland Hills, Los Angeles County, California, USA
    Posts
    9,643
    Rep Power
    4248
    Originally Posted by Akshat1
    Also, what is the difference between is-a and has-a relationship in Python?
    is-a relationship means that the child class inherits the parent's methods. A has-a relationship means that a class contains an instance of another class, but does not inherit its methods.

    E.g.
    Code:
    class Parent:
        def method1(self):
             pass
        def method2(self):
             pass
    Now if you have a second class like this:
    Code:
    class Child(Parent):
    Now, Child will already have methods called method1 and method2, because the Parent class has them.

    However, if you wrote a class like this:
    Code:
    class has_a:
        def __init__(self):
             self.parent_class = Parent()
    Now the class has_a has a member called parent_class, but does not inherit the methods method1 and method2, since it is not a child of Parent(). Therefore, if I need to have class_a have two methods called method1() and method2(), then I need to write them (and have them delegate the actions to parent_class, if needed). E.g.
    Code:
    class has_a:
        def __init__(self):
             self.parent_class = Parent()
        def method1(self):
            return self.parent_class.method1()  # Delegation
        def method2(self):
            self.method2_count += 1   # Do something else first
            self.parent_class.method2()   # and delegate it
    Up the Irons
    What Would Jimi Do? Smash amps. Burn guitar. Take the groupies home.
    "Death Before Dishonour, my Friends!!" - Bruce D ickinson, Iron Maiden Aug 20, 2005 @ OzzFest
    Down with Sharon Osbourne

    "I wouldn't hire a butcher to fix my car. I also wouldn't hire a marketing firm to build my website." - Nilpo
  6. #4
  7. No Profile Picture
    Contributing User
    Devshed Newbie (0 - 499 posts)

    Join Date
    Feb 2013
    Posts
    100
    Rep Power
    2
    Originally Posted by Scorpions4ever
    is-a relationship....
    Code:
    # and delegate it
    Thank you soooooo much. You helped a ton! Just one more thing, if you may: what is delegation in Python?
    Last edited by Akshat1; June 19th, 2013 at 03:27 PM.
  8. #5
  9. No Profile Picture
    Contributing User
    Devshed Newbie (0 - 499 posts)

    Join Date
    Feb 2013
    Posts
    100
    Rep Power
    2
    Originally Posted by Scorpions4ever
    is-a relationship means that the child class inherits the parent's methods. ...
    # and delegate it
    [/code]
    I worked on it a hell lot and I got the delegating thinggy.
    Again, thanks a ton for your help.

IMN logo majestic logo threadwatch logo seochat tools logo