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.
Overview of OOP Terminology
Class − A user-defined prototype for an object that defines a set of attributes that characterize any object of the class. The attributes are data members (class variables and instance variables) and methods, accessed via dot notation.
Class variable − A variable that is shared by all instances of a class. Class variables are defined within a class but outside any of the class's methods. Class variables are not used as frequently as instance variables are.
Data member − A class variable or instance variable that holds data associated with a class and its objects.
Function overloading − The assignment of more than one behavior to a particular function. The operation performed varies by the types of objects or arguments involved.
Instance variable − A variable that is defined inside a method and belongs only to the current instance of a class.
Inheritance − The transfer of the characteristics of a class to other classes that are derived from it.
Instance − An individual object of a certain class. An object obj that belongs to a class Circle, for example, is an instance of the class Circle.
Instantiation − The creation of an instance of a class.
Method − A special kind of function that is defined in a class definition.
Object − A unique instance of a data structure that's defined by its class. An object comprises both data members (class variables and instance variables) and methods.
Operator overloading − The assignment of more than one function to a particular operator.
Create a Class
To create a class, use the keyword
class
:Example
Create a class named MyClass, with a property named x:
- class MyClass:x = 5print(MyClass)
Output:-
<class '__main__.MyClass'>Create Object
Now we can use the class named MyClass to create objects:
Example
Create an object named p1, and print the value of x:
- class MyClass:x = 5p1 = MyClass()print(p1.x)
Output-
5The __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:
Example
Create a class named Person, use the __init__() function to assign values for name and age:
class Person:def __init__(self, name, age):self.name = nameself.age = agep1 = Person("John", 36)print(p1.name)print(p1.age)Output-
John
36Object 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:
Example
Insert a function that prints a greeting, and execute it on the p1 object:
- class Person:def __init__(self, name, age):self.name = nameself.age = agedef myfunc(self):print("Hello my name is " + self.name)p1 = Person("John", 36)p1.myfunc()
Output-
Hello my name is John
Modify Object Properties
You can modify properties on objects like this:
Example
Set the age of p1 to 40:
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.age = 40
print(p1.age)
Output-
40Delete Object Properties
You can delete properties on objects by using the
del
keyword:Example
Delete the age property from the p1 object:
- class Person:def __init__(self, name, age):self.name = nameself.age = agedef myfunc(self):print("Hello my name is " + self.name)p1 = Person("John", 36)del p1.ageprint(p1.age)
Output-
Traceback (most recent call last):
File "demo_class7.py", line 13, in <module>
print(p1.age)
AttributeError: 'Person' object has no attribute 'age'Delete Objects
You can delete objects by using the
del
keyword:Example
Delete the p1 object:
- class Person:def __init__(self, name, age):self.name = nameself.age = agedef myfunc(self):print("Hello my name is " + self.name)p1 = Person("John", 36)del p1print(p1)
Output-
Traceback (most recent call last):
File "demo_class8.py", line 13, in <module>
print(p1)
NameError: 'p1' is not defined
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.
Example
Do you Know?
Progamming is what actually means ?What is software development ?
How we categorized software development in different manner.
C Plus Plus
Python
Java Development
Web Designing
Javascript
Mysql
Oracle