Python Class
Table Of Contents:
- What Is A Class?
- How To Declare A Class?
- The __init__() Method.
- The ‘self’ Keyword.
- What Is Inside A Class?
(1) What Is A Class?
- A class is an empty structure, where you put all of your variables and methods inside it.
- These variables will not have any value, at the time of class creation.
- It will be just a placeholder, where you will assign some values in the later stage.
- A class is a blueprint of your application, that you design before actually implementing it.
(2) How To Declare A Class?
- A class can be defined using the “class” keyword followed by the class name.
- A class can have multiple variables and methods inside it.
Syntax:
class ClassName:
# Statement-1
.
.
.
# Statement-N
Example-1
class Student:
name = 'Abhispa'
mark = 67
grade = 'A'
subject = 'Math'
school = 'IIT'
def details():
print(name,mark,grade,subject,school)
Note:
- Here we have a Student class, which contains student details like name, mark, grade, subject, and school names.
- Which is having one method called details(), which will print all the student details.
- If you execute this piece of code, you will not see any output.
- It is just a blueprint.
Example-2
class Employee:
name = 'Subrat'
designation = 'Engineer'
salary = 10000
company='Praudyog'
def details():
print(name,designation,salary,company)
Note:
- Here we have an Employee class, which contains student details like name, designation, salary, and company names.
- Which is having one method called details(), which will print all the employee details.
- If you execute this piece of code, you will not see any output.
- It is just a blueprint.
(3) The __init__() Method.
- In the previous examples, we have directly assigned values to the variable.
- But it’s not a good practice to hardcode the values inside the class, because if you hard code you can only store a single value.
- To dynamically assign values you need to use the __init__() method.
- __init__() method is used to assign values to the variables inside the class.
- __init__() method is always executed when the class is being called.
Example-1
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)
obj = Country('India','Delhi','100Cr','Hindi')
obj.details()
Name: India
Capital: Delhi
Population: 100Cr
Language: Hindi
Note:
- Here we have called the class by its name ‘Country‘ and passed the required values.
- In return, the class will create an object, which is ‘obj‘ here.
- I can use that object to access values and methods inside the Country class.
(4) The ‘self’ Keyword.
- The ‘self’ keyword is used as the first parameter for every method.
- Because internally obj.details() will become, Country.details(obj).
- So every time you call a method inside the class, by default the object will be passed as an argument.
- To receive that object we use the ‘self’ keyword.
Example-1
class A:
def __init__(self,msg):
self.msg = msg
def message(self):
print(self.msg)
obj1 = A('Good Morning')
obj2 = A('How Are You?')
obj3 = A('I Am Good!')
obj1.message()
obj2.message()
obj3.message()
Output:
Good Morning
How Are You?
I Am Good!
Note:
- Internally,
- obj1.message() = A.message(obj1)
- obj2.message() = A.message(obj2)
- obj3.message() = A.message(obj3)
(5) What Is Inside A Class?
A class is having mainly two components.
(1) Class Attributes.
(2) Class Behaviours.
Class Attributes:
- In procedural programming, we call values as variables.
- But, in the case of object-oriented programming, we call values as attributes.
Example
class Student:
name = 'Abhispa'
mark = 67
grade = 'A'
subject = 'Math'
school = 'IIT'
def details():
print(name,mark,grade,subject,school)
Note:
- Her name, mark, grade, subject, and school are the attributes of the Student class.
- This attribute defines a Student.
Class Behaviors:
- In procedural programming, we call functions as methods.
- But, in the case of object-oriented programming, we call functions as behaviours.
- Because it represents the behaviour of an object.
Example
class Person:
def __init__(self, name, age):
self.name = name
self.age = age
def details(self):
print('Name:',self.name)
print('Age:',self.age)
obj = Person("John", 36)
obj.details()
Output:
Name: John
Age: 36
Note:
- Here function details() called as the behaviour of the class Person.