We Offer 100% Job Guarantee Courses (Any Degree / Diploma Candidates / Year GAP / Non-IT / Any Passed Outs). Placement Records
Hire Talent (HR):+91-9707 240 250

General

Init in Python

Init in Python

Init in Python

“__init__” is defined as an object of an In-build method in the class of python. __init__  method is also defined as to be an object-oriented concept (Oop’s) of a constructor and also it is created from the python’s class and it is allowing the class to be initialized the attributes of classes and __init__method  is defined by an object.

Class

A class is a group of variables, methods, In-build function of set the indifferent category of elements also can called as a class in Python is to be a blueprint of exact behavior for the individual objects with different or same.

Object

An object is one of the instances of a class and their methods and it can be performed by the functions which is to be defined as in the python class.

Self

In Python self is a keyword that is used to define all the instances in a python class. You can be easily accessed a defined of all the instances in a python class, methods and attributes.

Let’s consider an example for __init__ which will be that you are creating a school database. for that, we could behave, students, details. Students  is an attributes like to be “subjects”, “name”, “age” ,”address”. and the methods like be “marks”, “absent”, “present”, “rank” etc.

class students():
def __init__(self, name,age, address,):
self.name= name
self.age= age
self.address= address
Let try with some data to have for students class as below,
Besant=students(‘xyz’,24,’abc’)
GangBoard=students(‘’mln’,26,’uvw’)

We have to be creating two different types of students class objects with the same students class. while student class created with object of passing arguments  “xyz”, “24”, “abc”  these are the arguments to be passing in “__init__” method to-be initialized with an object of Besant and also other student class created with object of passing arguments  “’mln’”, “26”, “uvw”  these are the arguments to be passing in “__init__” method to-be initialized with an object of GangBoard.

then, the keyword “self”  is representing the instance of the student class. It also to be binds of the many attributes with arguments of parsing the given value. The use of “self” is to be a class to be accessing the multiple methods and their attributes of arguments.

Example 1:

>>> class A(Object)
def_init_(self, something)
print("A init called")
self.something = something
>>> class B(A)
def_init_(self, something) 
print("B init called")
self.something = something
#calling init of parent class
A.init_(self, something)

Output:

>>obj = B("something")
B init called
A init called
>>>

Example 2:

>>> class student (object)
def _ init_(self, something)
print ("Init called,")
self.something = something
def method (self)
return self. something

Output:

>>my_object = student('python')
Init Called
>>

One more example Find out the volume of a cylinder field with height(h=150), radius(r=100).

class cylinder:
def __init__(self, height, radius):
self.height = height
self.radius = radius
def get_volume_of_cylinder(self):
return (self.height *self.radius*self.radius*3.14)
def get_area_of_cylinder(self):
return (self.height + self.radius)*(2*self.radius*3.14)

And the above example is related to be “self” represented to be the same object of the cylinder and the instance of the cylinder class. If you are getting into the method “get_area_of_cylinder” we have to be get the “self.height”  with a value of attribute of “height” and “self.radius”  with a value of attribute of “radius” and If you are getting into the method “get_volume_of_cylinder” we have to get the “self.height”  with a value of attribute of “height” and “self.radius”  with a value of attribute of “radius”. If you are passing a value a height(h=150), radius(r=100) of class cylinder .

x=cylinder(150,100)

Then the value is taken the first argument as height to be 150 and radius to be 100and if you are accessing the value for “get_area_of_cylinder” which will return as 157000.0 and f you are accessing the value for “get_volume_of_cylinder” which will returns as 4710000.0.

Program Executed in IDLE

>>> class cylinder
def init_(self, height, radius)
self.height = height
self.radius = radius
def get volume of_cylinder(self)
return (self.height * self.radius *self.radius*3.14)
def get area of_cylinder(self)
return (self.height + self.radius) * (2*self.radius*3.14)

Output:

>>>x=cylinder(150,160)
>>> x.get_area_of_cylinder()
157000.0
>>>x.get_volume_of_cylinder()
4710000.0
>>>

 

Related Blogs:

  1. Brief Overview of Python Language
  2. Python Career opportunities
  3. Python Break Continue
  4. Python Control Flow
  5. Python Data Types
  6. Python Dictionary
  7. Python Exception Handling
  8.  Python File
  9. Python Functions
  10. Python Substring
Besant Technologies WhatsApp