8 Replies - 1198 Views - Last Post: 07 September 2012 - 08:20 PM Rate Topic: -----

#1 Akasen  Icon User is offline

  • New D.I.C Head

Reputation: 1
  • View blog
  • Posts: 41
  • Joined: 13-August 12

Help with Polymorphism in Python

Posted 07 September 2012 - 05:19 PM

So I've had a buddy of mine who programs who has attempted to teach me how to program for some months now. By teach I mean I teach myself stuff and only mine him when I need help to see if he can actually help. This time around, he's asked of me to have a firmer understanding on OOP. I seem to have a decent understanding of things, except for Polymorphism.

Now, I'm not exactly sure what he's asking for, or if him coming from a background of C and C++ trying to teach me programming through Python is a problem, but I've sent to him my work on Polymorphism concepts and he's asked for me to make a "static function called Reward that I pass the Person object to and inside of Reward will make them talk."

Not really getting what he's asking. Anyone mind giving me some pointers?

class Person:
    def __init__(self, name):
        self.name = name


def Reward(Person):
    pass

class Guy(Person):
    def talk(self):
        return 'Righteous!'

class Dude(Person):
    def talk(self):
        return 'Thanks man!'

class Bob(Person):
    def talk(self):
        return '...!'

people = [Guy('Guy'),
          Dude('Dude'),
          Bob('Bob')]

for person in people:
    print person.name + ' yells, ' + person.talk()



Is This A Good Question/Topic? 0
  • +

Replies To: Help with Polymorphism in Python

#2 blackcompe  Icon User is offline

  • D.I.C Lover
  • member icon

Reputation: 1155
  • View blog
  • Posts: 2,533
  • Joined: 05-May 05

Re: Help with Polymorphism in Python

Posted 07 September 2012 - 06:03 PM

Quote

"static function called Reward that I pass the Person object to and inside of Reward will make them talk."


This doesn't make much sense (to me at least). A static function is a class method, meaning it can't access any instance data. It's not called on any specific instance. So if you were to write a static function it would need to accept two Persons and make them talk.

    @staticmethod
    def static_reward(person1, person2):
         pass
    .
    .
    .
    Person.static_reward(Bob('Bob'), Dude('Dude'))



Another reason this doesn't make much sense is because polymorphism has just about no relation to static methods. The above code is not polymorphic. If anything he should of told you to write an instance method, Reward, that accepts another Person, where that person is rewarded (i.e. they talk). Moreover, each subtype of Person should specialize Reward.

class Person:
    def __init__(self, name):
        self.name = name

    def reward(self, other):
        print "This is the superclass implementation..."
    	print self.name + ": I'm rewarding you."
        print other.name + ": " + other.talk() + "\n"

    @staticmethod
    def static_reward(person1, person2):
       print person1.name + ": I'm rewarding you."
       print person2.name + ": " + person2.talk() + "\n"

class Guy(Person):
    def talk(self):
        return 'Righteous!'

    def reward(self, other):
        print "This is the Guy implementation..."
    	print self.name + ": I'm rewarding you in my own special way."
        print other.name + ": " + other.talk() + "\n"

class Dude(Person):
    def talk(self):
        return 'Thanks man!'

    def reward(self, other):
        print "This is the Dude implementation..."
    	print self.name + ": I'm rewarding you in my own special way."
        print other.name + ": " + other.talk() + "\n"

class Bob(Person):
    def talk(self):
        return '...!'

    def reward(self, other):
        print "This is the Bob implementation..."
    	print self.name + ": I'm rewarding you in my own special way."
        print other.name + ": " + other.talk() + "\n"

people = [Guy('Guy'),
          Dude('Dude'),
          Bob('Bob')]

for person in people:
    person.reward(people[0])

#Person.static_reward(Bob('Bob'), Dude('Dude'))


Was This Post Helpful? 0
  • +
  • -

#3 Akasen  Icon User is offline

  • New D.I.C Head

Reputation: 1
  • View blog
  • Posts: 41
  • Joined: 13-August 12

Re: Help with Polymorphism in Python

Posted 07 September 2012 - 06:10 PM

Well truth be told, I based what I had upon the Wikipedia entry on Polymorphism for Python which looked like this.

class Animal:
    def __init__(self, name):    # Constructor of the class
        self.name = name
    def talk(self):              # Abstract method, defined by convention only
        raise NotImplementedError("Subclass must implement abstract method")
 
class Cat(Animal):
    def talk(self):
        return 'Meow!'
 
class Dog(Animal):
    def talk(self):
        return 'Woof! Woof!'
 
animals = [Cat('Missy'),
           Dog('Lassie')]
 
for animal in animals:
    print animal.name + ': ' + animal.talk()
 
# prints the following:
#
# Missy: Meow!
# Lassie: Woof! Woof!


Was This Post Helpful? 0
  • +
  • -

#4 atraub  Icon User is offline

  • Pythoneer
  • member icon

Reputation: 759
  • View blog
  • Posts: 2,010
  • Joined: 23-December 08

Re: Help with Polymorphism in Python

Posted 07 September 2012 - 06:17 PM

surprisingly, much of the Python code I've seen on Wikipedia has been awful.
Was This Post Helpful? 0
  • +
  • -

#5 blackcompe  Icon User is offline

  • D.I.C Lover
  • member icon

Reputation: 1155
  • View blog
  • Posts: 2,533
  • Joined: 05-May 05

Re: Help with Polymorphism in Python

Posted 07 September 2012 - 06:21 PM

Quote

Well truth be told, I based what I had upon the Wikipedia entry on Polymorphism for Python which looked like this.


Ah, I didn't even realize the talk method was polymorphic. It is indeed. But whatever your friend is asking of you needs to be clarified.
Was This Post Helpful? 0
  • +
  • -

#6 Akasen  Icon User is offline

  • New D.I.C Head

Reputation: 1
  • View blog
  • Posts: 41
  • Joined: 13-August 12

Re: Help with Polymorphism in Python

Posted 07 September 2012 - 06:25 PM

View Postblackcompe, on 08 September 2012 - 01:21 AM, said:

Quote

Well truth be told, I based what I had upon the Wikipedia entry on Polymorphism for Python which looked like this.


Ah, I didn't even realize the talk method was polymorphic. It is indeed. But whatever your friend is asking of you needs to be clarified.

Wait, how is talk polymorphic?

Also I'm starting to lose hope in my friend. Truth be told, his very attitude is very demeaning and it just makes me feel like he thinks I'm stupid.
Was This Post Helpful? 0
  • +
  • -

#7 blackcompe  Icon User is offline

  • D.I.C Lover
  • member icon

Reputation: 1155
  • View blog
  • Posts: 2,533
  • Joined: 05-May 05

Re: Help with Polymorphism in Python

Posted 07 September 2012 - 06:49 PM

Let me start by saying learning polymorphism with a dynamically-typed language as a first language is a really bad idea. The absence of types makes things unclear. So, take Java for instance -- this is polymorphic behavior. You have an array of Animals, which you know may be different types of animals, but you also know they have a talk method. When you call that method it calls the specific implementation.

for(Animal a: animals)



The variable a is polymorphic, meaning it can take many different forms. a can reference any subtype of Animal and a.talk may behave differently for each (if talk is overridden for that object).

So it's the same thing in python:

animals = [Cat('Missy'),
           Dog('Lassie')]
 
for animal in animals:
    print animal.name + ': ' + animal.talk()



You have an array of Animal objects, each of which could be a subtype of Animal, but you know they all have a talk method. So when you iterate over the array you just assume each element is an Animal and that Animal.talk will execute the specific implementation for that object.

If the code wasn't polymorphic, the method talk defined in the Animal class would execute for each of iteration of the loop and an exception would be raised.

The concept of polymorphism is IMO much clearer when you're working in a statically-typed language.

This post has been edited by blackcompe: 07 September 2012 - 06:53 PM

Was This Post Helpful? 1
  • +
  • -

#8 Akasen  Icon User is offline

  • New D.I.C Head

Reputation: 1
  • View blog
  • Posts: 41
  • Joined: 13-August 12

Re: Help with Polymorphism in Python

Posted 07 September 2012 - 07:54 PM

I showed my bud your explanation, blackcompe, and he IMMEDIATELY understood why I was having such trouble. Thanks man.
Was This Post Helpful? 0
  • +
  • -

#9 blackcompe  Icon User is offline

  • D.I.C Lover
  • member icon

Reputation: 1155
  • View blog
  • Posts: 2,533
  • Joined: 05-May 05

Re: Help with Polymorphism in Python

Posted 07 September 2012 - 08:20 PM

Sweet! Good luck.
Was This Post Helpful? 0
  • +
  • -

Page 1 of 1