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

    Join Date
    Feb 2013
    Posts
    100
    Rep Power
    2

    Someone explain polymorphism to me please?


    OK, from my tutorial (Learning Python 4th Edition by Mark Lutz), here's the code I have:
    Code:
    class Person:
        def __init__(self, name, job=None, pay=0):
            self.name = name
            self.job = job
            self.pay = pay
        def giveRaise(self, percent):
            self.pay = int(self.pay * (1 + percent))
        def __str__(self):
            return '[Person: %s, %s]' % (self.name, self.pay)
    
    class Manager(Person):
        def giveRaise(self, percent, bonus=.10): # Redefine at this level
            Person.giveRaise(self, percent + bonus) # Call Person's version
    
    
    if __name__ == '__main__':
        bob = Person('Bob Smith')
        sue = Person('Sue Jones', job='dev', pay=100000)
        tom = Manager('Tom Jones', 'mgr', 50000) # Make a Manager: __init__
        for object in (bob, sue, tom): # Process objects generically
            object.giveRaise(.10) # Run this object's giveRaise
            print(object) # Run the common __str__
    And here's the explanation:
    This is just Python’s notion of polymorphism, which we met earlier in the book, at work
    again—what giveRaise does depends on what you do it to. Here, it’s made all the more
    obvious when it selects from code we’ve written ourselves in classes. The practical effect
    in this code is that sue gets another 10% but tom gets another 20%, because
    giveRaise is dispatched based upon the object’s type.
    And that is crystal clear to me. But after that (in another chapter), it also says:
    Polymorphism
    In X.method, the meaning of method depends on the type (class) of X.
    That is very very confusing to me; in X.method, the meaning of method depends upon how the method is defined, right? Not upon whatever's happening with the class. :/ If someone would care to explain to me what that means, I'll really appreciate it.
    Thanks, in advance.
    Last edited by Akshat1; June 15th, 2013 at 05:44 AM.
  2. #2
  3. Contributing User
    Devshed Newbie (0 - 499 posts)

    Join Date
    Nov 2012
    Location
    Iran
    Posts
    149
    Rep Power
    139
    Hello,

    Originally Posted by Akshat1
    That is very very confusing to me; [SIZE=3]in X.method, the meaning of method depends upon how the method is defined, right?[/SIZE] Not upon whatever's happening with the class. :/ If someone would care to explain to me what that means, I'll really appreciate it.
    Thanks, in advance.
    It depends in fact on the implementation, but the problem is that the implementation of a given method in a parent class (Super class) and its child(s) (subclass) is not necessarily always the same, that is, when a subclass redefines a parent class methods according to the need and context of the subclass, then Python is capable to distinguish which method (the one defined in the parent or that defined in the subclass) is to be called.

    Here is an example, which might help you

    python Code:
    class SuperClass:
        def __init__(self):
            print("SuperClass constructor was called")
     
        def sayHello(self):
            print("I say Hello from the context of the SuperClass")
     
        def sayGoodBuy(self):
            print("No subclass redefined me")
     
     
    class SubClass(SuperClass):
        def __init__(self):
            print("SubClass constructor was called")
     
        def sayHello(self):
            print("I say Hello from the context of the SubClass")
     
     
     
     
    def main():
        objSuper = SuperClass()
        objSuper.sayHello()
        print("\n")
        objSub = SubClass()
        objSub.sayHello()
        objSub.sayGoodBuy()
     
     
    main()


    So if you run the above code here is the output

    Code:
    $ python -tt myTestScript.py
    SuperClass constructor was called
    I say Hello from the context of the SuperClass
    
    
    SubClass constructor was called
    I say Hello from the context of the SubClass
    No subclass redefined me
    $
    Regards,
    Dariyoosh
  4. #3
  5. No Profile Picture
    Contributing User
    Devshed Newbie (0 - 499 posts)

    Join Date
    Feb 2013
    Posts
    100
    Rep Power
    2
    Originally Posted by dariyoosh
    Python is capable to distinguish which method (the one defined in the parent or that defined in the subclass) is to be called.
    And that's what we would call polymorphism?
  6. #4
  7. No Profile Picture
    Contributing User
    Devshed Newbie (0 - 499 posts)

    Join Date
    Dec 2012
    Posts
    114
    Rep Power
    3
    That's one kind of polymorphism, yes.
  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 Nyktos
    That's one kind of polymorphism, yes.
    One kind of polymorphism means others referring to like:
    Code:
    # meaning of an operation depends on the object’s being operated on.
    1 * 5 # different result for int
    'string' * 5 # different result for string
    Right?
  10. #6
  11. Contributing User
    Devshed Newbie (0 - 499 posts)

    Join Date
    Nov 2012
    Location
    Iran
    Posts
    149
    Rep Power
    139
    Originally Posted by Akshat1
    One kind of polymorphism means others referring to like:
    Code:
    # meaning of an operation depends on the object’s being operated on.
    1 * 5 # different result for int
    'string' * 5 # different result for string
    Right?

    Maybe you want to read this

    http://en.wikipedia.org/wiki/Polymorphism_in_object-oriented_programming
    Regards,
    Dariyoosh

IMN logo majestic logo threadwatch logo seochat tools logo