0% found this document useful (0 votes)
26 views

Python Programming 2

Python is an object-oriented programming language where almost everything is an object. A class is a blueprint used to create these objects. To define a class, use the keyword "class" followed by the class name. Objects are then created from the class. The __init__() function is called automatically when an object is instantiated and is where initialization takes place. The __str__() function controls the string representation of an object. Methods are functions defined inside a class. Inheritance allows a child class to inherit properties and methods from a parent class.

Uploaded by

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

Python Programming 2

Python is an object-oriented programming language where almost everything is an object. A class is a blueprint used to create these objects. To define a class, use the keyword "class" followed by the class name. Objects are then created from the class. The __init__() function is called automatically when an object is instantiated and is where initialization takes place. The __str__() function controls the string representation of an object. Methods are functions defined inside a class. Inheritance allows a child class to inherit properties and methods from a parent class.

Uploaded by

misbahrajpoot606
Copyright
© © All Rights Reserved
Available Formats
Download as PDF, TXT or read online on Scribd
You are on page 1/ 9

Python Classes/Objects

Python is an object-oriented programming language.

Almost everything in Python is an object, with its properties and methods.

A Class is like an object constructor, or a "blueprint" for creating objects.

Create a Class
To create a class, use the keyword class:

Create Object
Now we can use the class named MyClass to create objects:
The __init__() Function
The examples above are classes and objects in their simplest form, and are not
really useful in real life applications.

To understand the meaning of classes we have to understand the built-in


__init__() function.

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 __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:
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:


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:

Modify Object Properties


You can modify properties on objects like this:
Delete Object Properties
You can delete properties on objects by using the del keyword:

Delete Objects
You can delete objects by using the del keyword:

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.
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.

Create a Parent Class


Any class can be a parent class, so the syntax is the same as creating any other
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:
Now the Student class has the same properties and methods as the Person
class.

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

When you add the __init__() function, the child class will no longer inherit the
parent's __init__() function.
To keep the inheritance of the parent's __init__() function, add a call to the
parent's __init__() function:

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.

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:

By using the super() function, you do not have to use the name of the parent
element, it will automatically inherit the methods and properties from its parent.

Add Properties

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 Methods

If you add a method in the child class with the same name as a function in the
parent class, the inheritance of the parent method will be overridden.

You might also like