Classes and Objects Objects Help are things Think
Classes and Objects
Objects Help are ‘things’. Think ‘nouns’. us organize code Defined by classes Classified Also into groups referred to as ‘instances’
Classes Parent classes and ‘children’ of those classes. Classes can be both parents and children (like your parents are both parents to you and children to your grandparents)
You stay classy, Mardela!
Class & Object Relationship: Things: Inanimate: Wood (obj) Animate: Animals Mammals Cows (obj)
Creating classes in Python class Animals(Animate): class Mammals(Animals): class Cows(Mammals): ***We tell Python that the class is a child of another class by naming the parent in parenthesis (like a last name!)
Adding objects to classes
We want to add Bessy to our Cow class Bessy is a cow and she is an object or instance of this class. Let’s introduce her to the Cows class. bessy = Cows( ) we can also add another cow to the Cows class Hilda hilda = Cows( )
Great, we have two cows, but what can they do? Well first, we need to add some characteristics to the classes. We do this with functions. Let’s think on the characteristics each class has…
Animals Parent class Mammals Cows Class of Animals Move Funct. of mammal Gives milk to young Funct. of mammal Class of Mammals Chews grass Funct. of cows Moo Funct. of Cows
Let’s add some character to our class Mammals: def move(self): print(“I am moving, I am moving…”) def give_milk_to_young(self): print(“The mother feeds her young”)
Let’s add some character to our class Cows: def chew_grass(self): print(“I am chewing, I am chewing…”) def moo(self): print(“The sound you hear is a Moooooo!”)
Mooooving an object… Invoking the (self) parameter
Inheritance… We type in self as the first parameter when defining our functions This is a characteristic of the Python program which allows us one function in the class to call another function within that same class, or another parent class. That’s all
Inheritance continued: Functions with a class, can use functions (methods) within a parent class as well. The abilities are ‘passed down’ or ‘passed through’ Can I borrow the car?
The pass statement: We can do a few things that will help us: We can use the pass statement. We can use this when we want to set up our program but may not know what to do yet. Begin our program by typing in the following: class Animals(Animate): pass
These functions can be thought of as actions, which that class can do Let’s make Bessy move, which is a function of her parent class, animals. (Remember, Bessy is an object in the class Cows(Mammals). Bessy. move() Even though move is a function of the parent class Animals, bessy, an object of the Cows class, which is a class of Mammals, which is a class of Animals, can use this function… this is an inherited function.
How everything is set up: class Mammals: def move(self ): print(“I am moving, I am moving…”) def give_milk_to_young(self ): print(“The mother feeds her young”) class Cows: def chew_grass(self ): print(“I chewing, I am chewing…”) def moo(self): print(“The sound you hear is a Moooooo!”) bessy = Cows( ) bessy. move() hilda = Cows( ) Hilda. moo( ) If you typed this and got an error because bessy can’t move. It is because we have not linked these two classes with a parent class.
Let’s practice by teaching our cow’s dance! Create a program where our cow Bessy dances (printing what happens) *jumps in place Cow class Objects bessy & hilda (whatever names you’d like)
- Slides: 19