0% found this document useful (0 votes)
20 views36 pages

05 Modularize

Uploaded by

t235912
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)
20 views36 pages

05 Modularize

Uploaded by

t235912
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/ 36

05 Modularize

in our previous examples, sometimes, our codes become large, and this will cause
problems in maintenance and readability

we should always modularize our codes!


Functions

functions are
re-usable, but putting
many functions in a
file is still problematic
Modules
in python, a .py file is a module

functions declared in a module can be imported into other modules


this introduce a module
lib.utils
be aware of the full name of
the function
Ways to Import
Classes
You can put functions and variables into modules, but sometimes, modules alone
are not enough. You may want to have fine-grained control of scopes and
instances.

Use classes.
● Classes provide a means of bundling data and functionality together.
● Creating a new class creates a new type of object, allowing new instances of
that type to be made.
● Each class instance can have attributes attached to it for maintaining its state.
Class instances can also have methods (defined by its class) for modifying its
state.

https://docs.python.org/3/tutorial/cla https://www.w3schools.com/python/
sses.html python_classes.asp
https://as1.ftcdn.net/v2/jpg/04/82
/41/76/1000_F_482417602_F4q
Mc75cVZgs0iRI4W8iibBzL0Y0a
JLN.jpg
class Person:
constructor
def __init__(self, name, age):
self.name = name
self.age = age

p1 = Person("John", 36)
instance

print(p1.name)
print(p1.age)
__init__
All classes have a function called __init__(), which is always executed when the
class is being initiated.

Use the __init__() function to assign values to object properties, or other


operations that are necessary to do when the object is being created.

The __init__() function is called automatically every time the class is being used to
create a new object.
__str__
The __str__() function controls what should be returned when the class object is
represented as a string.

If the __str__() function is not set, the string representation of the object is
returned.
what is self?
The self Parameter
The self parameter is a reference to the current instance of the class, and is used
to access variables that belongs to the class.

It does not have to be named self , you can call it whatever you like, but it has to
be the first parameter of any function in the class
Object Methods
Objects can also contain methods. Methods in objects are functions that belong to
the object.

object method
The pass Statement
class definitions cannot be empty, but if you for some reason have a class
definition with no content, put in the pass statement to avoid getting an error.
Class and Instance Variables
Generally speaking, instance variables are for data unique to each instance and
class variables are for attributes and methods shared by all instances of the class.
Private Variables
“Private” instance variables that cannot be accessed except from inside an object
don’t exist in Python.

However, there is a convention that is followed by most Python code: a name


prefixed with an underscore (e.g. _spam) should be treated as a non-public part of
the API (whether it is a function, a method or a data member).
Example
class Person:
def __init__(self, name):
self.name=name
self.weight=0

def eat(self, w):


self.weight+=w

def __str__(self):
return self.name+", weight="+str(self.weight)
p1=Person("p1")
p2=Person("p2")
p1.eat(10)
p2.eat(20)

print(p1)
print(p2)
Inheritance
The name BaseClassName must be defined in a namespace accessible from the
scope containing the derived class definition.

In place of a base class name, other arbitrary expressions are also allowed. This
can be useful, for example, when the base class is defined in another module:
Example

the constructor method


is also inherited
Add the __init__() Function
When you add the __init__() function, the child class will no longer inherit the
parent's __init__() function.

The child's __init__() function overrides the inheritance of the parent's __init__()
function.
Use the super() Function
Python also has a super() function that will make the child class inherit all the
methods and properties from its parent
Example
from lib.models import *
class Student(Person):
def __init__(self, name, grade):
super().__init__(name)
self.grade=grade

s=Student("lendle", 100)
s.eat(100)
print(s)

You might also like