#50 Python Tutorial for Beginners | __init__ method

#50 Python Tutorial for Beginners | __init__ method

Introduction to OOP in Python

In this section, the speaker introduces Object-Oriented Programming (OOP) concepts in Python. The speaker explains what objects and classes are and how they relate to each other.

Objects and Classes

  • An object has attributes (variables) and behavior (methods).
  • Methods are similar to functions but are called differently in OOP.
  • To work with methods, we need to define them within a class.

Special Method: init

  • The init method is a special method that acts as a constructor for a class. It initializes the attributes of an object when it is created.
  • The init method is automatically called when an object is created.
  • We can pass arguments to the init method to initialize the attributes of an object.

Defining Attributes

  • We can define attributes within the init method by accepting arguments and assigning them to variables using self.variable_name = argument_name syntax.
  • We can then access these attributes using dot notation on the object instance, e.g., obj.attribute_name.

Conclusion

In this section, we learned about OOP concepts in Python, including objects, classes, methods, and attributes. We also learned about the special init method used for initializing objects' attributes during creation time.

Passing Parameters to Objects

In this section, the speaker explains how to pass parameters to objects in Python.

Creating an Object with Parameters

  • An object needs to have a value because it is just an argument.
  • CPU and RAM are just arguments. If you want them to be part of your object, you can assign them using self.
  • Every object will have its own variables and methods that work together.

Using Self to Fetch Values

  • When referring to an object's variable, use self instead of a local variable.
  • The concept of binding data with every method is called encapsulation.
  • Every object will have its own values for variables like CPU and RAM.

Example Code

  • Running the code shows that each object has its own values for CPU and RAM.
Video description

Check out our courses: AI Powered DevOps with AWS - Live Course :- https://go.telusko.com/AIDevOps-AWS Coupon: TELUSKO10 (10% Discount) Complete Java Developer Course Batch-4: https://go.telusko.com/Complete4 Coupon: TELUSKO10 (10% Discount) Master Java Spring Development : https://go.telusko.com/masterjava Coupon: TELUSKO20 (20% Discount) Udemy Courses: Spring: https://go.telusko.com/udemyteluskospring Java:- https://go.telusko.com/udemyteluskojava Java Spring:- https://go.telusko.com/Udemyjavaspring Java For Programmers:- https://go.telusko.com/javaProgrammers Python : https://go.telusko.com/udemyteluskopython Git : https://go.telusko.com/udemyteluskogit Docker : https://go.telusko.com/udemyteluskodocker For More Queries WhatsApp or Call on : +919008963671 website : https://courses.telusko.com/ Instagram : https://www.instagram.com/navinreddyofficial/ Linkedin : https://in.linkedin.com/in/navinreddy20 TELUSKO Android App : https://bit.ly/TeluskoApp Discord : https://discord.gg/D8hWe9BqfF 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 Complete Python Tutorial for Beginners Playlist : https://www.youtube.com/watch?v=hEgO047GxaQ&t=0s&index=2&list=PLsyeobzWxl7poL9JTVyndKe62ieoN-MZ3 Python Tutorial in Hindi : https://www.youtube.com/watch?v=JNbup20svwU&list=PLk_Jw3TebqxD7JYo0vnnFvVCEv5hON_ew Github :- https://github.com/navinreddy20/Python- Java and Spring Framework For beginners with Spring Boot : - http://bit.ly/3LDMj8D Java Tutorial for Beginners (2023) :- http://bit.ly/3yARVbN Editing Monitors : https://amzn.to/2RfKWgL https://amzn.to/2Q665JW https://amzn.to/2OUP21a. Subscribe to our other channel: Navin Reddy : https://www.youtube.com/channel/UCxmk... Telusko Hindi : https://www.youtube.com/channel/UCitz... Donation: PayPal Id : navinreddy20 Patreon : navinreddy20 http://www.telusko.com/contactus