Python Class and Objects
Python Class and Objects
What is class and object ?
How we can implement class and object in python ?
class
:Example :
How we can access class variable (ex: total_sofa ) value ?
To create a object, we use:
Create an object named p1, and print the value of total_sofa:
Example :
p1 = Room()
print(p1.total_sofa)
How we put value in class ?
- by using def __init__():
- by using DOT(.) operator
Let we see by using def __init__() :
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
Create a class named Person, use the __init__() function to assign values for name and age:
Example :
class Person:
def __init__(self, name, age):
self.name = name
self.age = age
p1 = Person("John", 36)
print(p1.name)
__init__()
function is called automatically every time the class is being used to create a new object.Let we see by using DOT(.) Operator:
set the age p1.age = 40
Example :
p1.age = 40
self
parameter is a reference to the current instance of the class, and is used to access variables that belong to the class.Deleting object parameter :
Delete the age property from the p1 object:
del p1.age
Deleting object :
You can delete objects by using the del
keyword:
Delete the p1 object:
del p1
===================================================================
That's it for today hope you like please share it with your friends and family.
Next lecture we see how to work with class function by making one little project.
Thank You 😀😀
Comments
Post a Comment