question is i’m bit confused about the fill-in-the-blanks assignment given in ex42
## Animal is-a object (yes, sort of confusing) look at the extra credit
class Animal(object):
pass
## ?? Dog is-a Animal
class Dog(Animal):
def __init__(self, name):
## ?? Dog has-a name
self.name = name
## ?? Cat is-a Animal
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
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 Fish
class Salmon(Fish):
pass
## ?? Hailbut is-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.pet = satan
## ?? frank is-a employee
frank = Employee("Frank", 120000)
## ??
frank.pet = rover
## ?? flipper is-a Fish
flipper = Fish()
## ?? crouse is-a Salmon
crouse = Salmon()
## ?? harry is-a Hailbut
harry = Halibut()
as you can see i have fill most of them but not on the line 60 and 66
what i would wright is
on line 60: mary has-a pet Cat named Satan
on line 66: frank has-a pet Dog named Rover
is that right?
because i think that mary and frank both are having pet
and all other fill ups are right or not
i appreciate your explanation on the same
In the example mary has become an instance of the Person class.
I don’t know if one also can call mary an object (please correct me if I am wrong).
You can connect attributes to the object.
Like name in the Person class .
mary “got her name” like this.
mary = Person('Mary')
There is also a attribute called pet in the Person class.
In the class pet is set to None
Later it is set to ‘satan’ (which is also an instance of Cat)
But you can choose whatever you like (perhaps ‘Angel’ )
mary.pet = 'Angel'
Perhaps ‘Mary’ want to have a car.
Add to the class the attribute: car.
self.car = None
Then give Mary the car
mary.car = 'Tesla'
If you want to print it out type:
print(f"{mary.name} has a {mary.car} and a cat called {mary.pet}")
# the f-string is for Python 3.6 or higher.
as you said that mary is an instance ie the object of class Person and class Person has a attributes name and pet
pet is assign to None and we are passing value of name attribute Mary while creating instance of Person class which is mary
now here the thing that satan is an instance of Cat class and now value of attribute pet of an instance mary is set to satan
so what should happen
class Cat should run again and pass that name attribute of Cat class to an pet attribute an instance mary
i think it’s not
then what’s the point
why there is need to set an satan instance of Cat to an attribute pet of an instance mary
what is happening there, what is that line 61 doing and what you will right in fill up on line 60
hey
i realized that i’m asking wrong question when you pointed out
i think i am just over thinking about setting satan to mary.pet
because the way zed said to find the relation between both so i think that Cat class will run again and there will be relation
but one thing that now mary.pet is also an object of Cat class, right?
yes
i rather not say that we learned something,
it’s something we seen that we don’t know
that’s weird
but we are still not sure what is happening, at least i am not
Just to clarify here @divyang_patel, but when you do this:
mary.pet = satan
Are you thinking that this creates a new Cat object? It actually just makes a link–aka a reference–to the original satan object. It doesn’t re-rerun Cat().
Thank you @zedshaw for clearing my doubt
yes earlier i was thinking that satan will run Cat but later i figured that there is no Class call, it’s just an object assignment
Here is my attempt at filling out the blanks. Figure I build on this thread. I could not figure out how to edit it as nice as divyang_patel.
# Animal is-a object
class Animal(object):
pass
# Dog is-a animal
class Dog(Animal):
# Dog has-a __init__ function with params self and name
def __init__(self, name):
# Dog has-a name
self.name = name
class Cat(Animal):
def __init__(self, name):
self.name = name
# Person is-a objecty
class Person(object):
def __init__(self, name):
# Person has-a component variable called name
self.name = name
# Person has-a component variable called pet
self.pet = None
class Employee(Person):
def __init__(self, name, salary):
# Employee has-a inherited function super with params employee and self, called by __init__ with param name
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
rover = Dog("Rover")
# Satan is-a cat
satan = Cat("Satan")
# Mary is-a person
mary = Person("Mary")
# From pet, get Mary function, call it with paramentes self, satan
mary.pet = satan
# Frank is-a employee with parameters Frank and 120000
frank = Employee("Frank", 120000)
# from pet get the frank function, call it with params self and rover
frank.pet = rover
# Flipper is-a Fish
flipper = Fish()
# crouse is-a salmon
crouse = Salmon()
# harry is-a halibut
harry = Halibut()
From a quick glance @dansmit that looks right, but instead of “component variable” you’d say “attribute”. You also have this:
# From pet, get Mary function, call it with paramentes self, satan
mary.pet = satan
But, it’s not getting the anything from pet. You’ve made 3 errors here:
Get the pet attribute from mary. You have this backwards, so can you explain why?
Mary is not a function. Again, try to explain why you thought this.
You aren’t calling with parameters self, you are assigning it to the attribute pet of mary. Try to explain why you thought this.
I’m asking you to explain your thinking since that will help you figure out how you misunderstand what’s going on and get you to go back and look at the exercises that will help.
Thank you Zed for taking the time to review my answer.
Re-looking at it I do not know why I made that mistake. I think I have a hard time understanding attributes and when/who/why use the period. I have come across many hurdles because of placing the period in the wrong place. Most of the time I eventually “fall into” the correct iteration and can move on, but I am clearly missing something. Do you have a suggestion to review attributes or period use? I went on YouTube and found all sort of unhelpful videos.
I believe the correct explanation would be:
From mary, get the pet attribute and set it to satan.
Next, make one of the objects from this exercise, say person, and do this:
frank = Person('frank')
print(frank.__dict__)
You’ll see the name and age attributes in there. Now, try this:
print(frank.__dict__['name'])
print(frank.name)
All the . (dot) operator does is translate to dictionary syntax by going into the secret .__dict___ dictionary. It’s just saving you the trouble of typing .dict all the time. When you get into multiple inheritance it gets more complicated, but all the dot does is access a dict with some magic for you.
And the magic is just it looks in dict and a few other places.
Here’s me doing it:
>>> class Person(object):
... def __init__(self, name):
... self.name = name
...
>>> frank = Person('frank')
>>> frank.__dict__
{'name': 'frank'}
>>> frank.__dict__['name']
'frank'
>>> frank.name
'frank'
>>>