Object-oriented programming (OOP)
Object-oriented programming (OOP)
(OOP)
22-12-23
CLASSES
• Object-oriented programming is one of the most effective approaches
to writing software. In object-oriented programming you write classes
that represent real-world things and situations, and you create
objects based on these classes. When you write a class, you define
the general behavior that a whole category of objects can have.
• When you create individual objects from the class, each object is
automatically equipped with the general behavior; you can then give
each object whatever unique traits you desire.
• Making an object from a class is called instantiation, and you work
with instances of a class.
Creating and Using a Class
• You can model almost anything using classes.
• Let’s start by writing a simple class, Dog, that represents a dog—not
one dog in particular, but any dog.
• What do we know about most pet dogs?
• they all have a name and age.
• We also know that most dogs sit and roll over.
• Those two pieces of information (name and age) and those two
behaviors (sit and roll over) will go in our Dog class because they’re
common to most dogs.
Creating and Using a Class (continue)
• After our class is written, we’ll use it to make individual instances,
each of which represents one specific dog.
dog.py
The __init__() Method
• A function that’s part of a class is a method.
• Everything you learned about functions applies to methods as well;
the only practical difference for now is the way we’ll call methods.
• The __init__() method : is a special method Python runs
automatically whenever we create a new instance based on the Dog
class.
• This method has two leading underscores and two trailing
underscores, a convention that helps prevent Python’s default
method names from conflicting with your method names.
The __init__() Method (continue)
• We define the __init__() method to have three parameters: self,
name, and age.
• The self parameter is required in the method definition, and it must
come first before the other parameters.
• It must be included in the definition because when Python calls this
__init__() method later (to create an instance of Dog), the method
call will automatically pass the self argument.
• Every method call associated with a class automatically passes self,
which is a reference to the instance itself; it gives the individual
instance access to the attributes and methods in the class.
The __init__() Method (continue)
• When we make an instance of Dog, Python will call the __init__()
method from the Dog class. We’ll pass Dog() a name and an age as
arguments; self is passed automatically, so we don’t need to pass it.
• Whenever we want to make an instance from the Dog class, we’ll
provide values for only the last two parameters, name and age.
• The two variables defined each have the prefix self.
• Any variable prefixed with self is available to every method in the
class, and we’ll also be able to access these variables through any
instance created from the class.
The __init__() Method (continue)
• self.name = name takes the value stored in the parameter name and
stores it in the variable name, which is then attached to the instance
being created.
• The same process happens with self.age = age. Variables that are
accessible through instances like this are called attributes.
• The Dog class has two other methods defined: sit() and roll_over().
• Because these methods don’t need additional information like a
name or age, we just define them to have one parameter, self.
The __init__() Method (continue)
• The instances we create later will have access to these methods. OR
they’ll be able to sit and roll over.
• For now, sit() and roll_over() don’t do much. They simply print a
message saying the dog is sitting or rolling over. But the concept can
be extended to realistic situations: if this class were part of an actual
computer game, these methods would contain code to make an
animated dog sit and roll over.
Creating Classes in Python 2.7
The Dog class would be defined like this in
Python 2.7:
Making an Instance from a Class
• Think of a class as a set of instructions for how to make an instance.
• The class Dog is a set of instructions that tells Python how to make
individual instances representing specific dogs.
• Let’s make an instance representing a specific dog:
Making an Instance from a Class (continue)
• When Python reads this line my_dog = Dog('willie', 6),
• It calls the __init__() method in Dog with the arguments 'willie' and 6.
• The __init__() method creates an instance representing this particular
dog and sets the name and age attributes using the values we
provided.
• The __init__() method has no explicit return statement, but Python
automatically returns an instance representing this dog.
• We store that instance in the variable my_dog.
Accessing Attributes
• To access the attributes of an instance, you use dot notation.
• we access the value of my_dog’s attribute name by writing:
LAB 23-12-23
Working with
Classes and
Instances
• Let’s write a new class
representing a car.
Setting a
Default
Value for an
Attribute
Modifying Attribute Values
• You can change an attribute’s value in three ways:
1. you can change the value directly through an instance,
2. set the value through a method,
3. or increment the value (add a certain amount to it) through a method.
1. Single
Inheritance:
2. Multiple
Inheritance: