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

Python Classes

The document discusses Python classes and object-oriented programming concepts like inheritance, methods, and properties. It shows how to create a Person class with name and age properties and a printname method. It then creates a Student class that inherits from Person, allowing student objects to access properties and methods from the parent Person class.

Uploaded by

Raghav Gulati
Copyright
© © All Rights Reserved
Available Formats
Download as PPTX, PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
88 views

Python Classes

The document discusses Python classes and object-oriented programming concepts like inheritance, methods, and properties. It shows how to create a Person class with name and age properties and a printname method. It then creates a Student class that inherits from Person, allowing student objects to access properties and methods from the parent Person class.

Uploaded by

Raghav Gulati
Copyright
© © All Rights Reserved
Available Formats
Download as PPTX, PDF, TXT or read online on Scribd
You are on page 1/ 23

The __str__() Function

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

The string representation of an object WITHOUT the __str__()


function:
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)
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 __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()
.
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

Modify Object Properties


You can modify properties on objects like this:
Example
Set the age of p1 to 40:
p1.age = 40
Delete Objects
You can delete objects by using the del keyword:

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 .

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:

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

Use the Student class to create an object, and then execute the printname method:


x = Student("Mike","Olsen")
x.print

tname()
Add the __init__() function to the Student class:
class Student(Person):
  def __init__(self, fname, lname):
    #add properties etc.

Add the __init__() Function


So far we have created a child class that inherits the properties and methods from its parent.
We want to add the __init__() function to the child class (instead of the pass keyword).
Note: The __init__() function is called automatically every time the class is being used
Now we have successfully added the __init__() function, and kept the inheritance of the parent class, and we are ready to add functionality in the __init__() function.

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

x = Student("Mike", "Olsen", 2019)


print(x.graduationyear)
las
: s
Add a method called welcome to the Student 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):
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)

x = Student("Mike", "Olsen", 2019)


x.welcome()
class Person:
def __init__(self, name, age):
self.name = name
self.age = age

p1 = Person("John", 36)

print(p1.name)
print(p1.age)

The string representation of an object WITHOUT the __str__() function:

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()

You might also like