Location via proxy:   [ UP ]  
[Report a bug]   [Manage cookies]                
0% found this document useful (0 votes)
3 views

Object-oriented programming (OOP)

The document provides an overview of object-oriented programming (OOP), focusing on the creation and use of classes, specifically a Dog class, and the instantiation of objects. It explains the __init__() method for initializing object attributes, the concept of inheritance for creating specialized classes, and the organization of classes into modules for better code management. Additionally, it covers methods for modifying attributes and the various types of inheritance available in Python.

Uploaded by

sadikjr330
Copyright
© © All Rights Reserved
Available Formats
Download as PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
3 views

Object-oriented programming (OOP)

The document provides an overview of object-oriented programming (OOP), focusing on the creation and use of classes, specifically a Dog class, and the instantiation of objects. It explains the __init__() method for initializing object attributes, the concept of inheritance for creating specialized classes, and the organization of classes into modules for better code management. Additionally, it covers methods for modifying attributes and the various types of inheritance available in Python.

Uploaded by

sadikjr330
Copyright
© © All Rights Reserved
Available Formats
Download as PDF, TXT or read online on Scribd
You are on page 1/ 67

Object-oriented programming

(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.

• Let’s look at each of these approaches.


Modifying
an
Attribute’s
Value
Directly
Modifying an
Attribute’s
Value
Through a
Method
We can extend the
method
update_odometer()
to do additional
work every time the
odometer reading is
modified. Let’s add
a little logic to make
sure no one tries to
roll back the
odometer reading:
Incrementing
an Attribute’s
Value
Through a
Method
LAB 23-12-23
Inheritance
• You don’t always have to start from scratch when writing a class.
• If the class you’re writing is a specialized version of another class you
wrote, you can use inheritance. When one class inherits from
another, it automatically takes on all the attributes and methods of
the first class.
• The original class is called the parent class, and the new class is the
child class.
• The child class inherits every attribute and method from its parent
class but is also free to define new attributes and methods of its own.
The __init__() Method for a Child Class
• The first task Python has when creating an instance from a child class
is to assign values to all attributes in the parent class.
• To do this, the __init__() method for a child class needs help from its
parent class.
• As an example, let’s model an electric car.
• An electric car is just a specific kind of car, so we can base our new
ElectricCar class on the Car class we wrote earlier.
• Then we’ll only have to write code for the attributes and behavior
specific to electric cars.
Inheritance
Inheritance
classes special methods
1. __init__ method:
2. __str__ method:
3. __add__ method:
4. __len__ method:
5. __getitem__ and __setitem__ methods:
6. __call__ method:
Comparison Methods:
1. __eq__ method:
2. __lt__, __le__, __gt__, __ge__ methods:
Arithmetic Methods:
1. __add__, __sub__, __mul__, __truediv__ methods:
2. __radd__, __rsub__, __rmul__, __rtruediv__ methods:
Inheritance
• The super() function is a special function that helps Python make
connections between the parent and child class.
• super().__init__(make, model, year), This line tells Python to call the
__init__() method from ElectricCar’s parent class, which gives an
ElectricCar instance all the attributes of its parent class.
• The name super comes from a convention of calling the parent class a
superclass and the child class a subclass.
Inheritance in Python 2.7
Inheritance in Python 2.7
• The super() function needs two arguments: a reference to the child
class and the self object. These arguments are necessary to help
Python make proper connections between the parent and child
classes. When you use inheritance in Python 2.7, make sure you
define the parent class using the object syntax as well.
Defining
Attributes and
Methods for
the Child Class
Overriding Methods from the Parent Class
• You can override any method from the parent class that doesn’t fit
what you’re trying to model with the child class.
• you define a method in the child class with the same name as the
method you want to override in the parent class.
• Python will disregard the parent class method and only pay attention
to the method you define in the child class.
• Say the class Car had a method called fill_gas_tank(). This method is
meaningless for an all-electric vehicle, so you might want to override
this method. Here’s one way to do that:
Overriding
Methods
from the
Parent Class
Instances as Attributes
• When modeling something from the real world in code, you may find that
you’re adding more and more detail to a class. You’ll find that you have a
growing list of attributes and methods and that your files are becoming
lengthy.
• In these situations, you might recognize that part of one class can be
written as a separate class. You can break your large class into smaller
classes that work together.
• For example, if we continue adding detail to the ElectricCar class, we might
notice that we’re adding many attributes and methods specific to the car’s
battery. When we see this happening, we can stop and move those
attributes and methods to a separate class called Battery. Then we can use
a Battery instance as an attribute in the ElectricCar class:
Instances as Attributes
Types of Inheritance
1. Single Inheritance:
2. Multiple Inheritance:
3. Multilevel Inheritance :
4. Hierarchical Inheritance:
5. Hybrid Inheritance:
Single inheritance enables
a derived class to inherit
properties from a single
parent class.

1. Single
Inheritance:
2. Multiple
Inheritance:

• When a class can be derived from more


than one base class this type of
inheritance is called multiple
inheritances.
3. Multilevel Inheritance :
• In multilevel inheritance, features of the base class and
the derived class are further inherited into the new
derived class. This is similar to a relationship
representing a child and a grandfather.
4. Hierarchical Inheritance:

• When more than one


derived class are
created from a single
base this type of
inheritance is called
hierarchical
inheritance.
5. Hybrid
Inheritance:
• Inheritance
consisting of
multiple types of
inheritance is called
hybrid inheritance.
Importing Classes
• As you add more functionality to your classes, your files can get long,
even when you use inheritance properly. In keeping with the overall
philosophy of Python, you’ll want to keep your files as uncluttered as
possible.
• To help, Python lets you store classes in modules and then import the
classes you need into your main program.
Importing a Single Class
• Let’s create a module containing just the Car class.
Importing a Single Class cont.…
• Now we make a separate file called my_car.py. This file will import the Car class and then
create an instance from that class:
Importing a Single Class cont.…
• Now we make a separate file called my_car.py. This file will import the Car class and then
create an instance from that class:
Storing Multiple Classes in a Module
• You can store as many classes as you need in a single module,
although each class in a module should be related somehow.
• The classes Battery and ElectricCar both help represent cars, so let’s
add them to the module car.py:
• Now we can make a new file called my_electric_car.py, import the
ElectricCar class, and make an electric car:
• Now we can make a new file called my_electric_car.py, import the
ElectricCar class, and make an electric car:
Importing Multiple Classes from a Module
• You can import as many classes as you need into a program file. If we
• want to make a regular car and an electric car in the same file, we
need to import both classes, Car and ElectricCar:
Importing Multiple Classes from a Module
• You can import as many classes as you need into a program file. If we
• want to make a regular car and an electric car in the same file, we
need to import both classes, Car and ElectricCar:
Importing an Entire Module
• You can also import an entire module and then access the classes you
need using dot notation.
Importing All Classes from a Module
• You can import every class from a module using the following syntax:

You might also like