Python Classes and Objects
A 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 in Python
In python to create a class, we have used a keyword called class:
Syntax:
class Class_Name:
statement
Example:
Now we will create a class named called MyClass, with a property named value.
class MyClass:
value = 10
Create Object in Python
Now we will use a class name as MyClass to create an object.
Syntax:
class ClassName: variable = value obj = ClassName()
Example:
class MyClass:
value = 10
obj = MyClass()
print(obj.value)
Output:
C:\Users\Pramod\PycharmProjects\pythonProject\venv\Scripts\python.exe C:/Users/Pramod/PycharmProjects/pythonProject/main.py
10
Process finished with exit code 0
The __init__() Function:
The example which is given above is class and object in the simplest way and is not really useful in real-life applications.
To know the meaning of classes we have to know 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 allocate values to object properties or other operations that are necessary to do when the object is being built:
Example:
class Student:
def __init__(self, s_name, s_age):
self.name = s_name
self.age = s_age
s1 = Student('Amresh', 22)
print(s1.name)
print(s1.age)
Output:
C:\Users\Pramod\PycharmProjects\pythonProject\venv\Scripts\python.exe C:/Users/Pramod/PycharmProjects/pythonProject/main.py
Amresh
22
Process finished with exit code 0
Object Methods:
An object is simply a collection of data and methods that act on those data.
Let’s create a method in the Student Class:
Example:
class Student:
def __init__(self, s_name, s_age):
self.name = s_name
self.age = s_age
def myfunc(self):
print('Hellow, My name is'+self.name)
s1 = Student('Amresh', 22)
s1.myfunc()
Output:
C:\Users\Pramod\PycharmProjects\pythonProject\venv\Scripts\python.exe C:/Users/Pramod/PycharmProjects/pythonProject/main.py
Hellow, My name isAmresh
Process finished with exit code 0
How is self used in Python?
The self parameter is used to reference the current instance of the class, use to exceed the value that belongs to the class. You do not have to name yourself, you can call whatever you like, but it has to be a first parameter on the function in the class.
Example:
class Student:
def __init__(my_self, s_name, s_age):
my_self.name = s_name
my_self.age = s_age
def myfunc(cse):
print('Hellow, My name is'+cse.name)
s1 = Student('Amresh', 22)
s1.myfunc()
Output:
C:\Users\Pramod\PycharmProjects\pythonProject\venv\Scripts\python.exe C:/Users/Pramod/PycharmProjects/pythonProject/main.py
Hellow, My name isAmresh
Process finished with exit code 0
How to modify Object Properties?
You can modify properties on objects in this way: s1.age = 24
Example:
class Student:
def __init__(self, s_name, s_age):
self.name = s_name
self.age = s_age
def myfunc(self):
print('Hellow, My name is'+self.name)
s1 = Student('Amresh', 22)
s1.age = 24
print(s1.age)
Output:
C:\Users\Pramod\PycharmProjects\pythonProject\venv\Scripts\python.exe C:/Users/Pramod/PycharmProjects/pythonProject/main.py
24
Process finished with exit code 0
How to Delete Object Properties?
You can delete the properties from an object by using the del keyword:
Example:
class Student:
def __init__(self, s_name, s_age):
self.name = s_name
self.age = s_age
def myfunc(self):
print('Hellow, My name is'+self.name)
s1 = Student('Amresh', 22)
del s1.age
print(s1.age)
Output:
C:\Users\Pramod\PycharmProjects\pythonProject\venv\Scripts\python.exe C:/Users/Pramod/PycharmProjects/pythonProject/main.py
Traceback (most recent call last):
File "C:/Users/Pramod/PycharmProjects/pythonProject/main.py", line 9, in <module>
print(s1.age)
AttributeError: 'Student' object has no attribute 'age'
Process finished with exit code 1
Pass Statement:
pass statement is used to avoid getting an error. When we define class then It should not be empty but if due to any reason class has defined with no content, at that time you can use a pass statement.
Example:
class Student:
pass
Recommended Post:
- Python Hello World Program
- Python Comment | Creating a Comment | multiline comment | example
- Python Dictionary Introduction | Why to use dictionary | with example
- How to do Sum or Addition in python
- Python Reverse number
- find the common number in python
- addition of number using for loop and providing user input data in python
- Python Count char in String
- Python Last Character from String
- Python Odd and Even | if the condition
- Python Greater or equal number
- Python PALINDROME NUMBER
- Python FIBONACCI SERIES
- Python Dictionary | Items method | Definition and usage
- Python Dictionary | Add data, POP, pop item, update method
- Python Dictionary | update() method
- Delete statement, Looping in the list In Python
- Odd and Even using Append in python
- Python | Simple Odd and Even number
Get Salesforce Answers