Python - Classes and Objects
Classes and Objects are basic concepts of Object Oriented Programming which revolve around the real life entities. Class. A class is a user defined blueprint or prototype from which objects are created. It represents the set of properties or methods that are common to all objects of one type.
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.
To create a class, use the keyword class :
class Myclass:
x=9
print(Myclass)
=====o/p====
<class '_main_.Myclass'>
Now we can use the class named myClass to create objects:
class Myclass:
x="freedom"
p1=Myclass()
print(p1.x)
====o/p====
freedom
class Person:
def __init__(self, name, age):
self.name = name
self.age = age
x = Person("Jay", 36)
print(x.name)
print(x.age)
======o/p======
Jay
36
Note: The __init__() function is called automatically every time the class is being used to create a new object.
Objects can also contain methods. Methods in objects are functions that belong to the object.
Let us create a method in the Person class:
class Person:
def __init__(x, name, age):
x.name = name
x.age = age
def myfunc(x):
print("Hello my name is " + x.name)
y = Person("Johanes", 36)
y.myfunc()
=====o/p======
Hello my name is Johanes
Note: The self parameter is a reference to the current instance of the class, and is used to access variables that belong to the class.
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__(freedom, name, age):
freedom.name = name
freedom.age = age
def myfunc(abc):
print("Hello my name is " + abc.name)
p1 = Person("Janush", 36)
p1.myfunc()
======o/p=====
Hello my name is Janush
Another program of same type:
class person:
def _init_(freedom,name,age):
freedom.name = name
freedom.age = age
def myfunc(y):
print("hello my name is" + y.name)
x = person("Nikil",56)
x.myfunc()
=====o/p=====
hello my name isNikil
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("rehman",36)
p1.age = 40
print(p1.age)
=====o/p=====
40
You can delete properties on objects by using the 'del' keyword :
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("rehman",36)
del p1.age
print(p1.age)
======o/p=====
Traceback (most recent call last):
File "C:\Users\Admin\Desktop\DATATYPES.py", line 10, in <module>
print(p1.age)
AttributeError: 'person' object has no attribute 'age'
You can delete objects by using the 'del' keyword:
Delete 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("Joy", 56)
del p1
print(p1)
======o/p=====
Traceback (most recent call last):
File "C:\Users\Admin\Desktop\DATATYPES.py", line 10, in <module>
print(p1)
NameError: name 'p1' is not defined
class definitions cannot be empty, but if we have a class definition with no content for some reason, put in the pass statement to avoid getting an error.
class Person:
pass
# having an empty class definition like this, would raise an error without the pass statement