Python Classes
Python Classes
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:
Example
p1 = Person("John", 36)
print(p1)
The string representation of an object WITH the __str__()
function:
class Person:
def __init__(self, name, age):
self.name = name
self.age = age
def __str__(self):
return f"{self.name}({self.age})"
p1 = Person("John", 36)
print(p1)
Object Methods
Objects can also contain methods. Methods in objects are functions that
belong to the object.
Let us create a method in the Person class:
p1 = Person("John", 36)
p1.myfunc()
Note: The self parameter is a reference to the current instance of the class, and is used to access variables that belo
Object Methods
class Person: Objects can also contain methods. Methods in objects are
functions
def __init__(self, name, that belong to the object.
age):
self.name = name Let us create a method in the Person class:
self.age = age
def myfunc(self):
print("Hello my name is " + self.name)
p1 = Person("John", 36)
p1.myfunc()
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 beNote: The self parameter
named self , you can is acall it whatever
reference you instance
to the current like, but it has
of the toand
class, beisthe
usedfirst parameter
to access variablesof any
that function in the class:
belong
to the classclass Person:
def myfunc(abc):
print("Hello my name is " + abc.name)
p1 = Person("John", 36)
p1.myfunc()
.
Delete Object Properties
You can delete properties on objects by using the del keyword:
Example
Delete the age property from the p1 object:
del p1.age
Example
Delete the p1 object:
del p1
he pass Statement
lass definitions cannot be empty, but if you for some reason have a class definition with no content, put in
xample
Python Inheritance
Inheritance allows us to define a class that inherits all the methods and properties
from another class.
Parent class is the class being inherited from, also called base class.
Child class is the class that inherits from another class, also called derived class.
class Person:
def __init__(self, fname, lname):
self.firstname = fname
self.lastname = lname
def printname(self):
print(self.firstname, self.lastname)
#Use the Person class to create an object, and then execute the printname method:
x = Person("John", "Doe")
x.printname()
Use the pass keyword when you do not want to add any other properties or methods to the class .
Example
Create a class named Student, which will inherit the properties and methods from the Person class:
class Student(Person):
pass
Create a class named Student, which will inherit the properties and methods from the Person clas
class Student(Person):
pass
tname()
Add the __init__() function to the Student class:
class Student(Person):
def __init__(self, fname, lname):
#add properties etc.
xample
class Student(Person):
def __init__(self, fname, lname):
Person.__init__(self, fname, lname)
Example
class Student(Person):
def __init__(self, fname, lname):
super().__init__(fname, lname)
class Person:
def __init__(self, fname, lname):
self.firstname = fname
self.lastname = lname
def printname(self):
print(self.firstname, self.lastname)
class Student(Person):
def __init__(self, fname, lname):
super().__init__(fname, lname)
x = Student("Mike", "Olsen")
x.printname()
Add a property called graduationyear to the Student class:
class Student(Person):
def __init__(self, fname, lname):
super().__init__(fname, lname)
self.graduationyear = 2019
In the example below, the year 2019 should be a variable, and passed into the Student class when creating student
objects.
To do so, add another parameter in the __init__() function:
Add a year parameter, and pass the correct year when creating objects:
class Student(Person):
class Student(Person):
def __init__(self, fname, lname, year):
super().__init__(fname, lname)
self.graduationyear = year
x = Student("Mike", "Olsen", 2019)
class Person:
def __init__(self, fname, lname):
self.firstname = fname
self.lastname = lname
def printname(self):
print(self.firstname, self.lastname)
class Student(Person):
def __init__(self, fname, lname, year):
super().__init__(fname, lname)
self.graduationyear = year
class Person:
def __init__(self, fname, lname):
self.firstname = fname
self.lastname = lname
def printname(self):
print(self.firstname, self.lastname)
class Student(Person):
def __init__(self, fname, lname, year):
super().__init__(fname, lname)
self.graduationyear = year
def welcome(self):
print("Welcome", self.firstname, self.lastname, "to the class of", self.graduationyear)
p1 = Person("John", 36)
print(p1.name)
print(p1.age)
class Person:
def __init__(self, name, age):
self.name = name
self.age = age
p1 = Person("John", 36)
print(p1)
The string representation of an object WITH the __str__()
function:
class Person:
def __init__(self, name, age):
self.name = name
self.age = age
def __str__(self):
return f"{self.name}({self.age})"
p1 = Person("John", 36)
print(p1)
Insert a function that prints a greeting, and execute it on
the p1 object:
class Person:
def __init__(self, name, age):
self.name = name
self.age = age
def myfunc(self):
print("Hello my name is " + self.name)
p1 = Person("John", 36)
p1.myfunc()
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:
class Person:
def __init__(mysillyobject, name, age):
mysillyobject.name = name
mysillyobject.age = age
def myfunc(abc):
print("Hello my name is " + abc.name)
p1 = Person("John", 36)
p1.myfunc()
Create a class named Person, with firstname and lastname properties, and a printname method:
class Person:
def __init__(self, fname, lname):
self.firstname = fname
self.lastname = lname
def printname(self):
print(self.firstname, self.lastname)
#Use the Person class to create an object, and then execute the printname method:
x = Person("John", "Doe")
x.printname()
Create a Child Class
To create a class that inherits the functionality from another class, send the parent class as a parameter
when creating the child class:
class Student(Person):
pass
Use the pass keyword when you do not want to add any other properties or methods to the class.
class Person:
def __init__(self, fname, lname):
self.firstname = fname
self.lastname = lname
def printname(self):
print(self.firstname, self.lastname)
class Student(Person):
pass
x = Student("Mike", "Olsen")
x.printname()