05 Modularize
05 Modularize
in our previous examples, sometimes, our codes become large, and this will cause
problems in maintenance and readability
functions are
re-usable, but putting
many functions in a
file is still problematic
Modules
in python, a .py file is a module
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.
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.
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 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)