开发者

Learn Python the Hard Way 2nd Edition. Exercise 45 [closed]

Closed. This question needs details or clarity. It is not currently accepting answers.

Want to improve this question? Add details and clarify the problem by editing this post.

Closed 7 years ago.

Improve this question

I'm going through Learn Python the Hard Way 2nd Edition and I've just done the exercise where am suppose to write down the is-a/has-a relationships. Am able to use classes somehow but i find is-a/has-a confusing. So I have no ideas if what I've done is correct, any pointers will be appreciated. Thanks.

## Animal is-a object (yes, sort of confusing) look at the extra credit
class Animal(object):
    pass


## ?? Dog is-a object
class Dog(Animal):

    def __init__(self, name):
        ## ?? Dog has-a name
        self.name = name

## ?? Cat is-a object
class Cat(Animal):

    def __init__(self, name):
        ## ?? Cat has-a name
        self.name = name

class Person(object):

    def __init__(self, name):
        ## ?? Person has-a name
        self.name = name

        ## Person has-a pet of some kind
        sel开发者_开发知识库f.pet = None

## ?? Employee is-a object
class Employee(Person):

    def __init__(self, name, salary):
        ## ?? hmm  what is this strange magic? 
        super(Employee, self).__init__(name)
        ## ?? Employee has-a salary
        self.salary = salary

## ?? Fish is-a object
class Fish(object):
    pass

## ?? Salmon is-a object, type of a fish
class Salmon(Fish):
    pass

## ?? Halibut is-a object, type of a fish
class Halibut(Fish):
    pass

## rover is-a Dog
rover = Dog("Rover")

## ?? satan is-a Cat
satan = Cat("Satan")

## ?? mary is-a person
mary = Person("Mary")

## ?? mary has-a pet called satan
mary.pet = satan

## ?? frank is-a Employee on 120000 salary
frank = Employee("Frank", 120000)

## ?? frank has-a pet called rover
frank.pet = rover

## ?? flipper is Fish
flipper = Fish()

## ?? crouse is-a Salmon
crouse = Salmon()

## ?? harry is-a Halibut
harry = Halibut()


So you are close. I remember struggling with the OOP paradigm at first also, especially after learning some c. Anyway, OOP is really more common sense than it may seem... The only tricky bit here is the first one, which was given to you... Animal is an Object.

While you are correct that Dogs, Cats, and Employees are objects, they are more importantly Animals, Animals, and People respectively.

This exercise is really about a property of OOP, inheritance. For instance, the class hierarchy for Dog:

Object -> Animal -> Dog

A dog is both an Animal and an Object, and it 'has' all of the properties of both. Now the properties can be overridden in subclasses, but that's for later discussion.

I think you started to get this around the Halibut and Salmon section. They are both Objects and Fish.

The last bit I would say, is:

Frank is an Employee (and Person and Object) whose name is Frank and has a salary of 120000.

I hope that helped.


The 'Learn Python the Hard Way' book has shifted chapters around a bit, so although this used to be exercise 45, it is now exercise 42. This chapter on is-a and has-a relationships confused me too for a good while.

From what I understood this question is considered offtopic on Stack Overflow. However, when I was googling for help this was one of the first places I found any answers, so I'll just add my answer to this question. I'm not completely sure if you're supposed to answer 'bad' questions, but I am quite sure I'm not the only person struggling with this exercise ;).

My answers:

## Animal is-a object
class Animal(object):
    pass

## Dog is-a Animal (Animal is-a Object, so Dog is-a Object as well.)
class Dog(Animal):

    def __init__(self, name):
        ## Dog has-a name
        self.name = name

## Cat is-a Animal (Animal is-a Object, so Cat is-a Object as well.)
class Cat(Animal):

    def __init__(self, name):
        ## Cat has-a Name
        self.name = name

## Person is-a Object
class Person(object):

    def __init__(self, name):
        ## Person has-a name
        self.name = name

        ## Person has-a pet of some kind
        self.pet = None

## Employee is-a Person (and is-a Object, of course, everything is-a Object!)
class Employee(Person):

    def __init__(self, name, salary):
        ## ?? This is a reliable way to call the 
        ##     __init__ method from Employee's 'super Class' 
        ##      Person (including name and pet)

        super(Employee, self).__init__(name)

        ## Employee has-a salary
        self.salary = salary

## Fish is-a Object
class Fish(object):
    pass

## Salmon is-a Fish
class Salmon(Fish):
    pass

## Halibut is-a Fish
class Halibut(Fish):
    pass


## rover is-a Dog (with name 'Rover')
rover = Dog("Rover")

## satan is-a Cat (with name 'Satan')
satan = Cat("Satan")

## mary is-a Person (with name 'Mary')
mary = Person("Mary")

## mary has-a pet called satan.
mary.pet = satan

## frank is-a Employee with name "Frank" and salary 120000.
## Note: Because Employee is-a Person, frank falls under the 
## Person class too.
frank = Employee("Frank", 120000)

## frank (the Employee from above) has a pet named rover. Since 
## frank is-a Employee, which falls under (is-a) Person, this means 
## he has-a pet as well.
frank.pet = rover

## flipper is-a Fish
flipper = Fish()

## course is-a Salmon
crouse = Salmon()

## harry is-a Halibut
harry = Halibut()


For the most part, what you have is correct. You establish a base class (ie, Fish) and you extend or further define that base class with more details by inheriting it and adding qualities or more features. This is your "is-a" relationship.

Your Employee "is-a" Person, and also "has-a" Salary. Salary can be a separately defined object which a Person may or may not have. You can even move this around so other objects contain, or have, the same object, Salary:

class AnimalActor(Dog):
   def __init__(self, salary):
      self.__salary = salary

s = Salary(50000)
lassie = AnimalActor(s)

"lassie" "is-a" Dog and also "has-a" Salary.


I feel like you may have left a few out. "Is-a" relationships cascade all the way up the inheritance tree, we call these "holonyms". Whereas "has-a" relationships cascade all the way down the posessions tree, we call them "meronyms".

You should just remember to go all the way up/down the trees, but I think you've got the idea.

# Salmon is-a Fish
# Note also that Fish is-a object
class Salmon(Fish):
    pass

# rover is-a Dog
# Dog is-a Animal
# Animal is-a object
# Note also that rover has-a name "Rover"
rover = Dog("Rover")

# frank is-a Employee
# Employee is-a Person
# Person is-a object
# Note also that frank has-a name "Frank"
# and frank has-a salary 120000 (ballllin'! must be a CS major.)
frank = Employee("Frank", 120000)

There are a couple more you missed, try to go back and draw out the inheritance trees:

object -> Animal -> Dog -> rover('Rover')
                        -> lassie('Lassie')
        ...
        ...
                 -> Cat -> satan('Lucifer')
       -> Person -> mary("Mary", pet=satan)
        ...

Et cetera.

0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜