Initialization in Python: everything you need to know

Python is one of the most popular coding platforms in the industry today. From hobbyists to professionals, everyone uses Python to write code and make applications for mobile and web. As such a general platform, some aspects are not well known among users. The most important one is Init In Python. This article will help you explore this concept and follow the instructions below in detail.

  • Initialize with Python
  • Introduction to the initialization function
  • Use Init in Python


Introduction to the initialization function with Python
If you have been using Python for some time, then you will know that Python is an object-oriented programming language. This basically means that everything you create in the Python environment is called an object. Now, before we start exploring more information about the __init__ function in Python, let us first understand the basics.

class

A class in Python is a category or collection of one or more different elements grouped together. They have one or more similarities to each other, but are different from other classes in type, quality, and kind. In technical terms, we can define a class in Python as a blueprint of a single object with the same or precise behavior.

purpose

An object in Python is an instance of a class, which can be programmed to perform the functions defined in the class.

Oneself

The self in keyword in Python is used for all instances in the class. By using the self keyword, you can easily access all the instances defined in the class, including its methods and properties.

inside

__init__ is one of the methods reserved in Python. In object-oriented programming, it is called a constructor. When creating an object from a class, the __init__ method can be called, and access rights are required to initialize the properties of the class.

Use init in Python

From the definition of __init__ shared above, you now have some understanding of the exact function of the method. To further clarify this concept, let us look at an example.

#1 example

Purpose: To write a racing game called "NFS" in Python.

Solution: If you want to create a racing game using Python named "NFS", one of the basic objects you need to create is a single car. Each car you create in the game will have different attributes, such as color, speed, etc., as well as methods of shifting, accelerating, breaking, etc.

When you code this concept into the Python interpreter, it should look like this.

class Car(object):
"""
blueprint for car
"""
def __init__(self, model, color, company, speed_limit):
self.color = color
self.company = company
self.speed_limit = speed_limit
self.model = model
def start(self):
print("started")
def stop(self):
print("stopped")
def accelarate(self):
print("accelarating...")
"accelarator functionality here"
def change_gear(self, gear_type):
print("gear changed")
" gear related functionality here"
Now that we have created the objects, let’s move on to create the individual cars in the game.
maruthi_suzuki = Car("ertiga", "black", "suzuki", 60)
audi = Car("A6", "red", "audi", 80)

In the above example, we have created two different car models; one is the Suzuki Ertiga and the other is the Audi A6. Once these objects are successfully created, we can initialize them using the __init__ method to prepare for the next step .

In this example, we can also use the self method to represent different instances of the class, and bind the attributes with the given parameters. Using the self method will allow us to basically access the properties and methods created in the class.

#2 example

Purpose: Find the development cost of a rectangular area with width (b = 120) and length (l = 160). The cost of 1 square meter is 2000 INR.

Solution: Keeping in mind the steps shared in the previous example, the code for this particular example is shown below.

class Rectangle:
def __init__(self, length, breadth, unit_cost=0):
self.length = length
self.breadth = breadth
self.unit_cost = unit_cost
def get_perimeter(self):
return 2 * (self.length + self.breadth)
def get_area(self):
return self.length * self.breadth
def calculate_cost(self):
area = self.get_area()
return area * self.unit_cost
# breadth = 120 cm, length = 160 cm, 1 cm^2 = Rs 2000
r = Rectangle(160, 120, 2000)
print("Area of Rectangle: %s cm^2" % (r.get_area()))
print("Cost of rectangular field: Rs. %s " %(r.calculate_cost()))

As mentioned in the previous example, the self method represents instances and properties of the class. If you study carefully, you will find that we have used the self.length method to derive the value of the property length. The property length is already bound to this class, and we use the self method to represent objects in the same class.

We also used def get_area(self): method as a parameter in the above code. What does this mean, every time we call the method, it will automatically pass the first parameter and the other parameters in the method. Although this automation may seem small at first glance, it will save a lot of time and improve efficiency in the long run.

To further clarify this discussion, look at the example below.

r = rectangle (160, 120, 2000)

Note: "r" is the representation of objects outside the class, and "self" is the representation of objects inside the class.

Insert picture description here
The above are some video resources I collected, which helped me a lot in this process. If you don't want to experience the feeling that you can't find the information during self-study, no one answers your questions, and insists on giving up after a few days, you can join our deduction group [313782132], which has various software testing resources and technical discussions.

Insert picture description here
Finally: Welcome to follow the editor to receive a summary of the core knowledge of Python automated test engineers with a 300-page pdf document! Software testing technology exchange group: (313782132) The content of these materials are all the knowledge points that the interviewer must ask during the interview. The chapter includes many knowledge points, including basic knowledge, Linux essentials, Shell, Internet program principles, Mysql Database, package capture tool topics, interface testing tools, advanced testing-Python programming, Web automation testing, APP automation testing, interface automation testing, advanced continuous integration testing, testing architecture development testing framework, performance testing, security testing, etc.

Guess you like

Origin blog.csdn.net/weixin_50271247/article/details/108650421