0% found this document useful (0 votes)
13 views13 pages

LN 10 ObjectNClass

Object and class

Uploaded by

miqie 03
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
13 views13 pages

LN 10 ObjectNClass

Object and class

Uploaded by

miqie 03
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
You are on page 1/ 13

TK2053

PROGRAMMING PARADIGM
Script Programming with Python

- Object and Class


2

Object and Class


• Python is an object oriented programming language.
• Everything is treated as an object be it functions, modules, data types etc.

• A class is simply a blueprint of a data that defines the characteristic


and behaviour of its data members and member function
• Object is an instance of the class
• Creating object is like defining a variable of class type

• Objects are used to store data and functions defined inside a


class
3

Object and Class


• A class doesn’t occupy any physical space in computer
memory until an object of that class type is defined.
• The declaration of class develops a template but data members cannot be
manipulated unless the object of its type is created.

• Attributes: data value which are stored inside an object

• Methods: the functions which are associated with the


object
4

Object and Class

Source: http://www.trytoprogram.com/python-programming/python-object-and-class/
5

Built-in Class
• Python has several built-in class including
• String
• Lists
• Dictionary
• and so on

• For example, a String class that makes string objects such as ‘cat’
and ‘dog’

• To identify the types of objects

- Note the use of the word ‘class’ instead of ‘type’


- A specific literal from one of these classes is
referred to as an instance of the class
6

User-defined Class
• When you write a class
• You defined the general behaviour of whole category of objects can
have

• When you create (instantiate) objects from class


• Each object is equipped with the general behaviour
• You can give each object whatever unique traits desire

• Classes can be
• Typed directly into programs
• Stored in modules and brought into programs with an import
statement
7

User-defined Class - Example


1
2

Each instance created from the Dog class will store a name and an age, and
each dog will have the ability to sit() and roll_over():
8

User-defined Class - Example


1• Define a class called Dog. Conventionally capitalized names refer to classes.

2• Docstring describing what the class does.

3• The __init__() method is a special method Python runs automatically


whenever we create new instance based on the Dog class. *The two leading
underscores and two trailing underscores is a convention that helps prevent Python’s default
method names from conflicting with your method names.

The self parameter is required as the first parameter in the method definition.
Each method's self parameter references the object; it gives the individual
instance access to the attributes and methods in the class.

4• The two variable defined have the prefix self. Any variable prefixed with self is
available to every method in the class, and also accessible through any
instance created from the class. These variables are called attributes.
9

User-defined Class - Example


5• The two other methods defined are sit() and roll_over(). They only have one
parameter, self, since they do not need additional information like name or age.
The instances created later will have access to these methods.
10

User-defined Class - Example


To make an instance from a class
• Think of class as a set of instructions for how to make an instance

1
2

1• Tell Python to create a dog name ‘willie’ age 6 in the variable my_dog. When
Python reads this line, it calls the __init__() method to creates an instance
representing this particular dog and sets the name and age attributes using
the values provided.

2• To access the attributes of an instance, use the dot notation, my_dog.name.


This is the same attribute referred to as self.name in the class Dog.
11

User-defined Class - Example


To call the methods in an instance

• To call a method, give the name of the instance (in this case, my_dog) and the
method you want to call, separated by a dot. When Python reads my_dog.sit(),
it looks for the method sit() in the class Dog and runs that code.
12

User-defined Class - Example


To create multiple instances

• Each dog is a separate instance with its own set of attributes, capable of the
same set of actions.
13

Hands-on Activities
• Make a class called User. Create two attributes called first_name and
last_name, and then create several other attributes that are typically
stored in a user profile. Make a method called describe_user() that
prints a summary of the user’s information. Make another method
called greet_user() that prints a personalized greeting to the user.

Create several instances representing different users, and call both


methods for each user.

You might also like