Python Objects
Table Of Contents:
- What Is A Python Object?
- How To Create Python Objects?
- Accessing Class Members Using Python Object.
- Deleting Attributes & Objects.
(1) What Is A Python Object?
- A Python object is a combination of data and methods, these methods are used to do some operations on data.
- As in real life, everything is seen as an object like a table, fan, mouse, keyboard, computer etc.
- So, Python is an Object Oriented Programming language, where everything is considered an object.
- A class is a blueprint of an object, for example, we can create a blueprint or sketch of a house.
- Blueprint will contain all the details about the floors, doors, windows etc.
- Based on these descriptions we build the house. House is an object.
- As many houses can be made from a house’s blueprint, we can create many objects from a class.
(2) How To Create A Python Object?
- We can create an Object of a class using the class name, followed by an open and close parenthesis ().
- You can pass arguments to the class inside the parenthesis().
- You can give any name to your object, by following the naming rules.
Example-1
class Addition:
def __init__(self, a,b):
self.a = a
self.b = b
def add(self):
sum = self.a + self.b
return sum
Creating Addition Object:
obj = Addition(10,20)
obj.add()
Output:
30
Note:
- We have created an Addition() object by calling Addition() class.
- ‘obj’ is the object of the Addition() class.
Example-2
class Country:
def __init__(self,name,capital,population,language,):
self.name = name
self.capital = capital
self.population = population
self.language = language
def details(self):
print('Name:',self.name)
print('Capital:',self.capital)
print('Population:',self.population)
print('Language:',self.language)
Creating Country Object:
obj = Country('India','Delhi','100Cr','Hindi')
obj.details()
Output:
Name: India
Capital: Delhi
Population: 100Cr
Language: Hindi
Note:
- We have created a Country() object by calling Country() class.
- ‘obj’ is the object of the Country() class.
(3) Accessing Class Members Using Python Object?
- You can easily access the class members using the object name and dot (.) operator.
Example-1
class Employee:
company = 'Praudyog'
def __init__(self,name):
self.name = name
def setsalary(self,salary):
self.salary = salary
def getsalary(self):
return self.salary
Creating Employee Object:
abhispa = Employee('Abhispa')
Accessing Members Of The Class:
print('Company:',abhispa.company)
print('Name:',abhispa.name)
abhispa.setsalary(100000)
print('Salary:',abhispa.getsalary())
Output:
Company: Praudyog
Name: Abhispa
Salary: 100000
(4) Deleting Attributes & Objects.
- ‘del’, the keyword, is used to delete any class attribute and the objects themselves.
Example-1
class Fruits:
def __init__(self,name,weight,price):
self.name = name
self.weight = weight
self.price = price
def details(self):
print('Name:',self.name)
print('Weight:',self.weight)
print('Price:',self.price)
Creating Fruits Object:
apple = Fruits('Apple','1kg',150)
Deleting Members Of The Class:
del apple.weight
Accessing Members Of The Class After Deleting:
apple.details()
Output:
AttributeError Traceback (most recent call last)
Input In [95], in <cell line: 1>()
----> 1 apple.details()
Input In [92], in Fruits.details(self)
6 def details(self):
7 print('Name:',self.name)
----> 8 print('Weight:',self.weight)
9 print('Price:',self.price)
AttributeError: 'Fruits' object has no attribute 'weight'