filmov
tv
#50 Python Tutorial for Beginners | __init__ method

Показать описание
Check out our courses:
Coupon: TELUSKO10 (10% Discount)
For More Queries WhatsApp or Call on : +919008963671
In this lecture we will learn:
- How to define variables in a class?
- What is __init__() in python?
- What is the use of the init() method?
- Creation of objects in Python
- How to set and extract values from a class?
#1
- Every object has two things: Attributes and Behaviour.
- Attributes are variables and behaviour is methods that are similar to functions.
- In python, variables will be defined in a special method known as __init__ that should be present inside the class.
- init() method must have an argument known as self with it.
def __init__(self):
- init() method in python works similarly to constructors that are present in other programming languages.
- init() runs automatically as we do not need to call this method.
- All methods other than init() will be executed only when they are called from somewhere in a program.
- For every object, init() method will be get called once. So, if we create two objects then the init() method will execute two times.
#2
- When an object of a class is created, the class is said to be instantiated. It is also known as object creation.
variable= class_name()
An object of that class will be created.
- We can pass arguments to a class in the constructor itself for their execution.
- Values of arguments can be accepted in a class in the init() method as its parameters.
- The variable name of the object is passed automatically and gets accepted by the self in the init() method.
class Computer:
def __init__(self, CPU, ram):
--(statements)--
com1=Computer('i5', '16')
#3
- So, in actuality when the object is created, first we pass the object itself in an argument and then pass values of other parameters in it.
- Whatever values you passed for different parameters in an object, will be assigned to an object through the self.
def __init__(self, CPU, ram):
- In a class variables are not local variables as they are associated with an object so we have to use the self with variables to fetch the values.
- We bind data with every object so one object will have its own methods and variables.
Python Tutorial to learn Python programming with examples
Editing Monitors :
Subscribe to our other channel:
Telusko Hindi :
Donation:
PayPal Id : navinreddy20
Patreon : navinreddy20
Coupon: TELUSKO10 (10% Discount)
For More Queries WhatsApp or Call on : +919008963671
In this lecture we will learn:
- How to define variables in a class?
- What is __init__() in python?
- What is the use of the init() method?
- Creation of objects in Python
- How to set and extract values from a class?
#1
- Every object has two things: Attributes and Behaviour.
- Attributes are variables and behaviour is methods that are similar to functions.
- In python, variables will be defined in a special method known as __init__ that should be present inside the class.
- init() method must have an argument known as self with it.
def __init__(self):
- init() method in python works similarly to constructors that are present in other programming languages.
- init() runs automatically as we do not need to call this method.
- All methods other than init() will be executed only when they are called from somewhere in a program.
- For every object, init() method will be get called once. So, if we create two objects then the init() method will execute two times.
#2
- When an object of a class is created, the class is said to be instantiated. It is also known as object creation.
variable= class_name()
An object of that class will be created.
- We can pass arguments to a class in the constructor itself for their execution.
- Values of arguments can be accepted in a class in the init() method as its parameters.
- The variable name of the object is passed automatically and gets accepted by the self in the init() method.
class Computer:
def __init__(self, CPU, ram):
--(statements)--
com1=Computer('i5', '16')
#3
- So, in actuality when the object is created, first we pass the object itself in an argument and then pass values of other parameters in it.
- Whatever values you passed for different parameters in an object, will be assigned to an object through the self.
def __init__(self, CPU, ram):
- In a class variables are not local variables as they are associated with an object so we have to use the self with variables to fetch the values.
- We bind data with every object so one object will have its own methods and variables.
Python Tutorial to learn Python programming with examples
Editing Monitors :
Subscribe to our other channel:
Telusko Hindi :
Donation:
PayPal Id : navinreddy20
Patreon : navinreddy20
Комментарии