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

Lesson 6 Classes and OOP

This lesson introduces Object-Oriented Programming (OOP) in Python, focusing on the concept of classes as blueprints for creating objects. It covers the structure of a class, including constructors, variables, and functions, and explains how to create and manipulate objects from these classes. Additionally, it discusses modules, name mangling, and special class attributes, providing a foundation for further exploration of OOP concepts.

Uploaded by

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

Lesson 6 Classes and OOP

This lesson introduces Object-Oriented Programming (OOP) in Python, focusing on the concept of classes as blueprints for creating objects. It covers the structure of a class, including constructors, variables, and functions, and explains how to create and manipulate objects from these classes. Additionally, it discusses modules, name mangling, and special class attributes, providing a foundation for further exploration of OOP concepts.

Uploaded by

Qombuter Agafari
Copyright
© © All Rights Reserved
Available Formats
Download as ODP, PDF, TXT or read online on Scribd
You are on page 1/ 44

Lesson 6: Introduction to Object-

Oriented Programming
Introduction to Python Programming
Lesson 6: Introduction to Object-
Oriented Programming

Lesson Overview

Introduction

Object-Oriented Programming

Adding More Class Functions

Modules

Review
Introduction

In the last lesson, you learned about functions.


You found out how to use functions to group and organize your code,

which makes your programs easier to write and use.



A class is another way to group and organize
code, making it more modular.
Classes are also the basis for object-oriented

programming (OOP).
Python is an object-oriented (OO) language.

3
Introduction


This lesson starts by defining a class and looking
at some descriptions of them.
You’ll then build your own class from the ground

up.

It'll start somewhat slow, but by the end of the
lesson, you'll have a fully functional class and a
much better understanding of why you might
want to use one of these in your programs.

4
Object-Oriented Programming: Building
the Blueprint

A class is a way to

group code, but it's


more than that.

A class gives you the
ability to write a
specification, or
blueprint, for how an
object created from
this class should look
and behave.

5
Object-Oriented Programming: Building
the Blueprint


Let's look at the words
blueprint, class, object,
when talking about
classes.

Car’s are made from
blueprints.

This document
specified things like
how the car's interior
and exterior should look
and where the doors
should go.
6
Object-Oriented Programming: Building
the Blueprint

You can do the same thing


with the classes in your


Python programs.

That is, you can write a
class that describes some
object. Python classes are
made from two basic
things: variables and
functions.

The variables hold the characteristics
of the object,

and the functions describe the
object's behavior.

7
Object-Oriented Programming: Building
the Blueprint


Take a minute and
imagine how you might
set up a Python class
that describes a car.

For its variables, you might use
things like number_of_doors,
exterior_color, and model_name.

Its functions might be things
like accelerate( ), turn_left( ), and
turn_on_lights( ).

8
Object-Oriented Programming: Building
the Blueprint


Then, every time you
want to use a car
object, you would
instantiate one (that is,
build one) based on the
class you already
established.

You can create as many objects
from a single class as you want,
which is exactly how blueprints
work.

9
Object-Oriented Programming: Creating
a Class to Tell Time


Let’s look an example-
a time class.

What are the
characteristics of time?

It can be described with just
two or three different numbers:
the hour, the minute, and
(optionally) the second.

10
Object-Oriented Programming: Creating
a Constructor


The first thing you need in order to build a
class is a constructor.

The class constructor is a special function that
is defined inside the class.
This function creates, or constructs, the object.


That usually means that you'll put code in the constructor to set the
object's variables to some default value.

For the Time class example, you’ll use the
constructor to set the hour, the minute, and the
second variables to 0. This is how it would look:

11
Object-Oriented Programming: Creating
a Constructor


Here’s how it looks:

The first line starts
with the keyword
class, followed by the
name of the class, and
then a colon.

The next line, with
the text enclosed
inside the triple
quotes, is the doc
string for this class.

12
Object-Oriented Programming: Creating
a Constructor


The next line has the
keyword, def followed
by __init__

This is Python's
keyword for the class
constructor. Every
class constructor will
have this name, no
matter the name of
your class.

13
OOP: The Importance of self


The self parameter is often called the class instance
object, or the object reference argument, or simply
the object reference.

It is used to gain access to the object's variables.

All class functions, including the constructor, must
specify at least this one parameter.
You can use a single class to create as many objects

as you want.

This is the exact reason why you need to use self inside the class definition:
so that Python knows which object's variables it should be accessing.

14
OOP: The Importance of self


Notice how each of the
variables must have self in
front of it.

Failure to put this in will
make Python think you're
referring to a global variable
instead of a variable that
belongs

It’s alsotoworth
the object.
noting that if you try to type this code out
and run it, you won’t get any output.
That’s because you are just using the class to define a

new data type. This is like telling Python how every Time
object you will create later will look and behave. You
haven’t actually created any objects yet.

15
Adding More Class Functions: More
Useful Functions


The next function we will
add is a set() function.

Notice how this code
takes the local parameter
value on the right side of
the equal sign and stores
that value in the object's
variable on the left side.

For example, hour is the local
parameter value and self.hour is
the value to store in the object
variable.

16
Adding More Class Functions: More
Useful Functions


Next we will add a
function to get the
variable values stored in
our object. Usually
referred to as a get
function.

Since the object is
keeping track of the
time – we just need to
pass the self parameter
to it.

17
Adding More Class Functions: More
Useful Functions


Our class will look like this:-

At this point, you have written a basic Time class. It


has a constructor, a way to set the values of the


object's variables, and a way to print these values.

18
Creating an Object


You can create an object
from your Time class
with a single line of code.

This line lists the
variable name of the new
Time object, an equal
sign, and the name of
the class.

The parentheses are
empty because Python
passes the self
parameter implicitly.
19
Creating an Object


To further test that out,
print the values of the
object's variables by
calling the print_time
function using
myTime1.print_time()

You’ll see that the
__init__ function has
initialized our object.

20
Creating an Object


You can also set the time by passing the values
to the object via the set() function.

21
Creating an Object


What do you think is wrong with this code?

22
Creating Additional Objects


You can use a class to
make as many objects
as you want.

23
Modules: Creating a Module for a Class


Usually the code that defines
the class and the code that
creates the objects from that
class are kept in different files.
Save the definition in Time.py

and include
from Time import Time
At the top of the rest of the

code which you can save in


<insert name>.py
The first Time refers to the

module name (the file name)


and the second to the class
name (the Time class we
defined)
24
Accessing Class Variables Directly

Python allows you to access the


object's variables directly, with


no restrictions.
Other object-oriented languages

enable you to create your class


variables with access restrictions
If direct access were not

allowed, then the only way the


hour field could be changed
would be through a function.

And if that were the case, then
you could write error-checking
code inside the function to
handle situations like the one in
the code to the right.

25
Accessing Class Variables Directly


Keep in mind that in real-
world programming
situations, the person who's
writing the class definition
may not be the same person
who creates objects from
that class.

That’s significant because
the person writing the
program may have no idea
how these classes are
implemented, what their
variable names are, or how
everything works.
26
Name Mangling


Although Python allows
direct access to an
object's variables, that
doesn't mean it's a good
idea.

In fact, a convention has
come up where
programmers will put two
underscore characters
(__) before a variable
name in their classes to
note that they shouldn't
be accessed directly.
27
Name Mangling


Notice that this code
includes a get_second
and set_second function
to work with this
variable.

This programming
strategy tells the Python
interpreter to perform
what is known as name
mangling. That is, now it
won't be able to access
each of the fields
directly.
28
Name Mangling


Attempting to run this
code will result in an
error. That's because
the interpreter has
used the name
mangling to give this
variable a new name.

This is Python’s way of
telling you that you
should be using the
class’ getter and setter
functions.
29
Cheating Your Way Around Name
Mangling


You can cheat around
the name mangling by
using this:
print (myTime1._Time__second)

Notice that there is
only one underscore
before Time but two
underscores after it.
With this syntax, you
will get the value stored
in __second.

30
Special Class Attributes


Python gives each class
a variety of special
attributes or specialized
information that you can
access through code.

__doc__

You can use this in your code to
get a listing of the documentation
string for the class.

31
Special Class Attributes


__class__

Another handy class attribute is
__class__. This attribute will give
you the class from which the object
was created.

The __class__ attribute can only be
accessed from an object attribute.

32
Some examples:

Write a class that represents a student. It must


have some attributes such as names and also


have some methods.

33
Some examples:


A possible solution: -

34
Some examples:


Write a Python class named Rectangle
constructed by a length and width and a method
which will compute the area of a rectangle.

35
Some examples:


Solution

36
Some examples:


Create a temperature class with a constructor ,
set methods that set C, F, K and do conversion,
as well as get methods.

37
Some examples:


Solution

38
Some examples:


Solution

39
Some examples:

40
Some examples:

41
Lesson 6 Review

This lesson began with a general overview of the concept of


classes, and you saw how similar classes are to blueprints.



Next, you learned how to write Python code to create a class
blueprint, including variables, constructors, and class
functions.
The next step was using these class blueprints to create

objects.
You saw first-hand how a single class could be used to create multiple objects.

You then discovered how to break your classes out into their own files to create modules.

You also learned how to hide your variables from other


programs via name mangling so that unintended errors don't


occur, and you learned some of the special attributes
available in Python.

44
Lesson 6 Review


The next lesson we will put these concepts of
modules and classes into action with something a
little more visual: graphics.

You’ll use modules that have been made available
by others that will enable you to create graphics
and even do some simple animation.

45
Some exercises:

46

You might also like