Hướng dẫn what is __ init __ in python class? - __ init __ trong lớp python là gì?

Trước khi hiểu các phương pháp "Bản thân" và "__init__" trong lớp Python, sẽ rất hữu ích nếu chúng ta có ý tưởng về một lớp và đối tượng.

Show

Lớp là một tập hợp hoặc danh mục của những thứ có một số thuộc tính hoặc thuộc tính chung và được phân biệt với các thuộc tính khác theo loại, loại hoặc chất lượng.

Về mặt kỹ thuật, chúng ta có thể nói rằng lớp là một bản in màu xanh cho các đối tượng riêng lẻ có hành vi chính xác.

Sự vật :

Đối tượng là một trong những trường hợp của lớp. có thể thực hiện các chức năng được xác định trong lớp.

bản thân :

Tự đại diện cho trường hợp của lớp. Bằng cách sử dụng từ khóa "Tự", chúng ta có thể truy cập các thuộc tính và phương thức của lớp trong Python.

__trong đó__ :

"__init__" là một phương pháp được phát triển trong các lớp Python. Nó được biết đến như một hàm tạo trong các khái niệm định hướng đối tượng. Phương thức này được gọi là khi một đối tượng được tạo từ lớp và nó cho phép lớp khởi tạo các thuộc tính của một lớp.

Làm thế nào chúng ta có thể sử dụng & nbsp; "__ init__"?

Hãy xem xét rằng bạn đang tạo một trò chơi NFS. Vì vậy, chúng ta nên có một chiếc xe hơi. Xe có thể có các thuộc tính như "màu", "công ty", "speed_limit", v.v. và các phương thức như "thay đổi_gear", "start", "tăng tốc", "di chuyển", v.v.

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"

Hãy cố gắng tạo ra các loại xe khác nhau

maruthi_suzuki = Car("ertiga", "black", "suzuki", 60)
audi = Car("A6", "red", "audi", 80)

Chúng tôi đã tạo ra hai loại đối tượng xe khác nhau với cùng một lớp. Trong khi tạo đối tượng xe, chúng tôi đã vượt qua các đối số & nbsp; "ertiga", "đen", "suzuki", 60 & nbsp; những đối số này sẽ chuyển sang phương thức "__init__" & nbsp; để khởi tạo đối tượng. & nbsp; "ertiga", "black", "suzuki", 60  these arguments will pass to "__init__" method  to initialize the object. 

Ở đây, từ khóa ma thuật "tự" & nbsp; đại diện cho thể hiện của lớp. Nó liên kết các thuộc tính với các đối số đã cho."self"  represents the instance of the class. It binds the attributes with the given arguments.

Việc sử dụng "bản thân" trong lớp để truy cập các phương thức và thuộc tính

Trường hợp: Tìm hiểu chi phí của một trường hình chữ nhật với chiều rộng (b = 120), chiều dài (L = 160). Nó có giá x (2000) & nbsp; rupee trên 1 đơn vị vuông

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()))

Như chúng ta đã thảo luận & nbsp; "tự" đại diện cho cùng một đối tượng hoặc thể hiện của lớp. Nếu bạn thấy, bên trong phương thức "get_area" & nbsp; chúng tôi đã sử dụng "self.length" & nbsp; để nhận giá trị của thuộc tính "độ dài". lớp) tại thời điểm tạo đối tượng. "Tự" & nbsp; đại diện cho đối tượng bên trong lớp. "tự" hoạt động giống như "r" & nbsp; trong câu lệnh & nbsp; "r = hình chữ nhật (160, 120, 2000)". & nbsp; Nếu bạn thấy cấu trúc phương thức "def get_area (self):" & nbsp; chúng tôi đã sử dụng "tự" & nbsp; làm tham số trong phương thức bởi vì bất cứ khi nào chúng tôi gọi phương thức & nbsp; đối tượng (ví dụ của lớp) Đối số cùng với các argumets khác của phương thức. Nếu không có đối số nào khác chỉ được cung cấp "Tự" được chuyển cho phương thức. Đó là lý do chúng tôi sử dụng "tự" để gọi phương thức bên trong lớp ("self.get_area ()"). & nbsp; chúng tôi sử dụng đối tượng (thể hiện của lớp) để gọi phương thức bên ngoài định nghĩa lớp ("r.get_area ()"). "R" & nbsp; là ví dụ của lớp, khi chúng ta gọi phương thức & nbsp; "r.get_area ()" & nbsp; & nbsp; phiên bản "r" & nbsp; được truyền như đối số đầu tiên ở vị trí của bản thân. & nbsp;"self" represents the same object or instance of the class. If you see, inside the method "get_area"  we have used "self.length" to get the value of the attribute "length".  attribute "length" is bind to the object(instance of the class) at the time of object creation. "self" represents the object inside the class. "self" works just like "r" in the statement  "r = Rectangle(160, 120, 2000)".  If you see the method structure "def get_area(self): "  we have used "self" as a parameter in the method because whenever we call the method  the object (instance of class) automatically passes as a first argument along with other argumets of the method.If no other arguments are provided only "self" is passed to the method. That's the reason we use "self" to call the method inside the class("self.get_area()").  we use object( instance of class) to call the method outside of the class definition("r.get_area()"). "r" is the instance of the class, when we call method "r.get_area()"  the instance "r" is passed as as first argument in the place of self.
 

r = Rectangle(160, 120, 2000)

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

Để biết thêm chi tiết, vui lòng truy cập tài liệu Python

Để biết thêm về gói nguồn mở Django CRM (Quản lý quan hệ khách hàng) của chúng tôi. Kiểm tra mã

__ init __ là gì (bản thân trong Python?Python Class, Objects, Self Whenever object-oriented programming is done in Python, we mostly come across __init__ method in oops which we usually don’t fully understand. This article explains the main concept of __init__ but before understanding the __init__ some prerequisites are required.

Bản thân trong từ khóa trong Python được sử dụng cho tất cả các trường hợp trong một lớp. Bằng cách sử dụng từ khóa tự, người ta có thể dễ dàng truy cập tất cả các trường hợp được xác định trong một lớp, bao gồm các phương thức và thuộc tính của nó. trong đó. __init__ là một trong những phương pháp dành riêng trong Python. Trong lập trình định hướng đối tượng, nó được gọi là một hàm tạo.

Điều kiện tiên quyết-Lớp Python, đối tượng, bản thân bất cứ khi nào lập trình hướng đối tượng được thực hiện trong Python, chúng tôi chủ yếu bắt gặp phương pháp __init__ trong OOPS mà chúng tôi thường không hiểu hoàn toàn. Bài viết này giải thích khái niệm chính của __init__ nhưng trước khi hiểu __init__ một số điều kiện tiên quyết được yêu cầu.in C++ and Java. Constructors are used to initializing the object’s state. The task of constructors is to initialize(assign values) to the data members of the class when an object of the class is created. Like methods, a constructor also contains a collection of statements(i.e. instructions) that are executed at the time of Object creation. It is run as soon as an object of a class is instantiated. The method is useful to do any initialization you want to do with your object.

Example:  

Python3

__Init__ trong Python là gì?

Chất xây dựng __init__ mặc định trong C ++ và Java. Các hàm tạo được sử dụng để khởi tạo trạng thái đối tượng. Nhiệm vụ của các hàm tạo là khởi tạo (gán giá trị) cho các thành viên dữ liệu của lớp khi một đối tượng của lớp được tạo. Giống như các phương thức, một hàm tạo cũng chứa một tập hợp các câu lệnh (nghĩa là hướng dẫn) được thực thi tại thời điểm tạo đối tượng. Nó được chạy ngay khi một đối tượng của một lớp được khởi tạo. Phương pháp này rất hữu ích để thực hiện bất kỳ khởi tạo nào bạn muốn làm với đối tượng của mình.

class Person:

maruthi_suzuki = Car("ertiga", "black", "suzuki", 60)
audi = Car("A6", "red", "audi", 80)
0____11
maruthi_suzuki = Car("ertiga", "black", "suzuki", 60)
audi = Car("A6", "red", "audi", 80)
2
maruthi_suzuki = Car("ertiga", "black", "suzuki", 60)
audi = Car("A6", "red", "audi", 80)
3
maruthi_suzuki = Car("ertiga", "black", "suzuki", 60)
audi = Car("A6", "red", "audi", 80)
4

maruthi_suzuki = Car("ertiga", "black", "suzuki", 60)
audi = Car("A6", "red", "audi", 80)
5
maruthi_suzuki = Car("ertiga", "black", "suzuki", 60)
audi = Car("A6", "red", "audi", 80)
3
maruthi_suzuki = Car("ertiga", "black", "suzuki", 60)
audi = Car("A6", "red", "audi", 80)
7
maruthi_suzuki = Car("ertiga", "black", "suzuki", 60)
audi = Car("A6", "red", "audi", 80)
8
maruthi_suzuki = Car("ertiga", "black", "suzuki", 60)
audi = Car("A6", "red", "audi", 80)
9

maruthi_suzuki = Car("ertiga", "black", "suzuki", 60)
audi = Car("A6", "red", "audi", 80)
0____11
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()))
22
maruthi_suzuki = Car("ertiga", "black", "suzuki", 60)
audi = Car("A6", "red", "audi", 80)
3
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()))
4

r = Rectangle(160, 120, 2000)

Note: "r" is the representation of the object outside of the class and "self"  is the representation of the object inside  the class.
7

Output:

Hello, my name is Nikhil

maruthi_suzuki = Car("ertiga", "black", "suzuki", 60) audi = Car("A6", "red", "audi", 80) 5class 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())) 6class 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())) 7class 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())) 8class 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())) 9maruthi_suzuki = Car("ertiga", "black", "suzuki", 60) audi = Car("A6", "red", "audi", 80) 3r = Rectangle(160, 120, 2000) Note: "r" is the representation of the object outside of the class and "self"  is the representation of the object inside  the class.1

r = Rectangle(160, 120, 2000)

Note: "r" is the representation of the object outside of the class and "self"  is the representation of the object inside  the class.
2
maruthi_suzuki = Car("ertiga", "black", "suzuki", 60)
audi = Car("A6", "red", "audi", 80)
8
r = Rectangle(160, 120, 2000)

Note: "r" is the representation of the object outside of the class and "self"  is the representation of the object inside  the class.
4
r = Rectangle(160, 120, 2000)

Note: "r" is the representation of the object outside of the class and "self"  is the representation of the object inside  the class.
5
r = Rectangle(160, 120, 2000)

Note: "r" is the representation of the object outside of the class and "self"  is the representation of the object inside  the class.
6init in python with parameters

Hiểu mã

Python3

__Init__ trong Python là gì?

Chất xây dựng __init__ mặc định trong C ++ và Java. Các hàm tạo được sử dụng để khởi tạo trạng thái đối tượng. Nhiệm vụ của các hàm tạo là khởi tạo (gán giá trị) cho các thành viên dữ liệu của lớp khi một đối tượng của lớp được tạo. Giống như các phương thức, một hàm tạo cũng chứa một tập hợp các câu lệnh (nghĩa là hướng dẫn) được thực thi tại thời điểm tạo đối tượng. Nó được chạy ngay khi một đối tượng của một lớp được khởi tạo. Phương pháp này rất hữu ích để thực hiện bất kỳ khởi tạo nào bạn muốn làm với đối tượng của mình.

class Person:

maruthi_suzuki = Car("ertiga", "black", "suzuki", 60)
audi = Car("A6", "red", "audi", 80)
0____11
maruthi_suzuki = Car("ertiga", "black", "suzuki", 60)
audi = Car("A6", "red", "audi", 80)
2
maruthi_suzuki = Car("ertiga", "black", "suzuki", 60)
audi = Car("A6", "red", "audi", 80)
3
maruthi_suzuki = Car("ertiga", "black", "suzuki", 60)
audi = Car("A6", "red", "audi", 80)
4

maruthi_suzuki = Car("ertiga", "black", "suzuki", 60)
audi = Car("A6", "red", "audi", 80)
5
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()))
6
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()))
7
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()))
8
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()))
9
maruthi_suzuki = Car("ertiga", "black", "suzuki", 60)
audi = Car("A6", "red", "audi", 80)
3
r = Rectangle(160, 120, 2000)

Note: "r" is the representation of the object outside of the class and "self"  is the representation of the object inside  the class.
1

A init called
B init called
2
maruthi_suzuki = Car("ertiga", "black", "suzuki", 60)
audi = Car("A6", "red", "audi", 80)
8
r = Rectangle(160, 120, 2000)

Note: "r" is the representation of the object outside of the class and "self"  is the representation of the object inside  the class.
4
r = Rectangle(160, 120, 2000)

Note: "r" is the representation of the object outside of the class and "self"  is the representation of the object inside  the class.
5
r = Rectangle(160, 120, 2000)

Note: "r" is the representation of the object outside of the class and "self"  is the representation of the object inside  the class.
6

A init called
B init called
7
maruthi_suzuki = Car("ertiga", "black", "suzuki", 60)
audi = Car("A6", "red", "audi", 80)
8
r = Rectangle(160, 120, 2000)

Note: "r" is the representation of the object outside of the class and "self"  is the representation of the object inside  the class.
4
B init called
A init called
0
r = Rectangle(160, 120, 2000)

Note: "r" is the representation of the object outside of the class and "self"  is the representation of the object inside  the class.
6

B init called
A init called
2
maruthi_suzuki = Car("ertiga", "black", "suzuki", 60)
audi = Car("A6", "red", "audi", 80)
8
r = Rectangle(160, 120, 2000)

Note: "r" is the representation of the object outside of the class and "self"  is the representation of the object inside  the class.
4
B init called
A init called
5
r = Rectangle(160, 120, 2000)

Note: "r" is the representation of the object outside of the class and "self"  is the representation of the object inside  the class.
6

B init called
A init called
7

B init called
A init called
8

B init called
A init called
9

Output:

Hello, my name is Nikhil
Hello, my name is Abhinav
Hello, my name is Anshul

__init__ với thừa kế

Kế thừa là khả năng của một lớp để lấy hoặc kế thừa các thuộc tính từ một số lớp khác. Hãy cùng xem xét ví dụ dưới đây để xem cách __init__ hoạt động trong kế thừa. & Nbsp;

Python3

class class1class224

____10

maruthi_suzuki = Car("ertiga", "black", "suzuki", 60)
audi = Car("A6", "red", "audi", 80)
1
maruthi_suzuki = Car("ertiga", "black", "suzuki", 60)
audi = Car("A6", "red", "audi", 80)
2
maruthi_suzuki = Car("ertiga", "black", "suzuki", 60)
audi = Car("A6", "red", "audi", 80)
3class8

maruthi_suzuki = Car("ertiga", "black", "suzuki", 60)
audi = Car("A6", "red", "audi", 80)
5
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()))
6
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()))
7Person:2
r = Rectangle(160, 120, 2000)

Note: "r" is the representation of the object outside of the class and "self"  is the representation of the object inside  the class.
6

maruthi_suzuki = Car("ertiga", "black", "suzuki", 60)
audi = Car("A6", "red", "audi", 80)
5
maruthi_suzuki = Car("ertiga", "black", "suzuki", 60)
audi = Car("A6", "red", "audi", 80)
3Person:6
maruthi_suzuki = Car("ertiga", "black", "suzuki", 60)
audi = Car("A6", "red", "audi", 80)
8 Person:8

class

maruthi_suzuki = Car("ertiga", "black", "suzuki", 60)
audi = Car("A6", "red", "audi", 80)
00

____10

maruthi_suzuki = Car("ertiga", "black", "suzuki", 60)
audi = Car("A6", "red", "audi", 80)
1
maruthi_suzuki = Car("ertiga", "black", "suzuki", 60)
audi = Car("A6", "red", "audi", 80)
2
maruthi_suzuki = Car("ertiga", "black", "suzuki", 60)
audi = Car("A6", "red", "audi", 80)
3class8

maruthi_suzuki = Car("ertiga", "black", "suzuki", 60)
audi = Car("A6", "red", "audi", 80)
5
maruthi_suzuki = Car("ertiga", "black", "suzuki", 60)
audi = Car("A6", "red", "audi", 80)
07
maruthi_suzuki = Car("ertiga", "black", "suzuki", 60)
audi = Car("A6", "red", "audi", 80)
3
maruthi_suzuki = Car("ertiga", "black", "suzuki", 60)
audi = Car("A6", "red", "audi", 80)
09

maruthi_suzuki = Car("ertiga", "black", "suzuki", 60)
audi = Car("A6", "red", "audi", 80)
5
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()))
6
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()))
7
maruthi_suzuki = Car("ertiga", "black", "suzuki", 60)
audi = Car("A6", "red", "audi", 80)
13
r = Rectangle(160, 120, 2000)

Note: "r" is the representation of the object outside of the class and "self"  is the representation of the object inside  the class.
6

maruthi_suzuki = Car("ertiga", "black", "suzuki", 60)
audi = Car("A6", "red", "audi", 80)
5
maruthi_suzuki = Car("ertiga", "black", "suzuki", 60)
audi = Car("A6", "red", "audi", 80)
3Person:6
maruthi_suzuki = Car("ertiga", "black", "suzuki", 60)
audi = Car("A6", "red", "audi", 80)
8 Person:8

class

maruthi_suzuki = Car("ertiga", "black", "suzuki", 60)
audi = Car("A6", "red", "audi", 80)
00

Output:

A init called
B init called

maruthi_suzuki = Car("ertiga", "black", "suzuki", 60)
audi = Car("A6", "red", "audi", 80)
20
maruthi_suzuki = Car("ertiga", "black", "suzuki", 60)
audi = Car("A6", "red", "audi", 80)
8
maruthi_suzuki = Car("ertiga", "black", "suzuki", 60)
audi = Car("A6", "red", "audi", 80)
222
maruthi_suzuki = Car("ertiga", "black", "suzuki", 60)
audi = Car("A6", "red", "audi", 80)
23
r = Rectangle(160, 120, 2000)

Note: "r" is the representation of the object outside of the class and "self"  is the representation of the object inside  the class.
6

Example:  

Python3

class class1class224

____10

maruthi_suzuki = Car("ertiga", "black", "suzuki", 60)
audi = Car("A6", "red", "audi", 80)
1
maruthi_suzuki = Car("ertiga", "black", "suzuki", 60)
audi = Car("A6", "red", "audi", 80)
2
maruthi_suzuki = Car("ertiga", "black", "suzuki", 60)
audi = Car("A6", "red", "audi", 80)
3class8

maruthi_suzuki = Car("ertiga", "black", "suzuki", 60)
audi = Car("A6", "red", "audi", 80)
5
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()))
6
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()))
7Person:2
r = Rectangle(160, 120, 2000)

Note: "r" is the representation of the object outside of the class and "self"  is the representation of the object inside  the class.
6

maruthi_suzuki = Car("ertiga", "black", "suzuki", 60)
audi = Car("A6", "red", "audi", 80)
5
maruthi_suzuki = Car("ertiga", "black", "suzuki", 60)
audi = Car("A6", "red", "audi", 80)
3Person:6
maruthi_suzuki = Car("ertiga", "black", "suzuki", 60)
audi = Car("A6", "red", "audi", 80)
8 Person:8

class

maruthi_suzuki = Car("ertiga", "black", "suzuki", 60)
audi = Car("A6", "red", "audi", 80)
00

____10

maruthi_suzuki = Car("ertiga", "black", "suzuki", 60)
audi = Car("A6", "red", "audi", 80)
1
maruthi_suzuki = Car("ertiga", "black", "suzuki", 60)
audi = Car("A6", "red", "audi", 80)
2
maruthi_suzuki = Car("ertiga", "black", "suzuki", 60)
audi = Car("A6", "red", "audi", 80)
3class8

maruthi_suzuki = Car("ertiga", "black", "suzuki", 60)
audi = Car("A6", "red", "audi", 80)
5
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()))
6
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()))
7
maruthi_suzuki = Car("ertiga", "black", "suzuki", 60)
audi = Car("A6", "red", "audi", 80)
13
r = Rectangle(160, 120, 2000)

Note: "r" is the representation of the object outside of the class and "self"  is the representation of the object inside  the class.
6

maruthi_suzuki = Car("ertiga", "black", "suzuki", 60)
audi = Car("A6", "red", "audi", 80)
5
maruthi_suzuki = Car("ertiga", "black", "suzuki", 60)
audi = Car("A6", "red", "audi", 80)
3Person:6
maruthi_suzuki = Car("ertiga", "black", "suzuki", 60)
audi = Car("A6", "red", "audi", 80)
8 Person:8

maruthi_suzuki = Car("ertiga", "black", "suzuki", 60)
audi = Car("A6", "red", "audi", 80)
5
maruthi_suzuki = Car("ertiga", "black", "suzuki", 60)
audi = Car("A6", "red", "audi", 80)
07
maruthi_suzuki = Car("ertiga", "black", "suzuki", 60)
audi = Car("A6", "red", "audi", 80)
3
maruthi_suzuki = Car("ertiga", "black", "suzuki", 60)
audi = Car("A6", "red", "audi", 80)
09

class

maruthi_suzuki = Car("ertiga", "black", "suzuki", 60)
audi = Car("A6", "red", "audi", 80)
00

Output:

B init called
A init called

maruthi_suzuki = Car("ertiga", "black", "suzuki", 60)
audi = Car("A6", "red", "audi", 80)
20
maruthi_suzuki = Car("ertiga", "black", "suzuki", 60)
audi = Car("A6", "red", "audi", 80)
8
maruthi_suzuki = Car("ertiga", "black", "suzuki", 60)
audi = Car("A6", "red", "audi", 80)
222
maruthi_suzuki = Car("ertiga", "black", "suzuki", 60)
audi = Car("A6", "red", "audi", 80)
23
r = Rectangle(160, 120, 2000)

Note: "r" is the representation of the object outside of the class and "self"  is the representation of the object inside  the class.
6
To know more about inheritance click here.


__ init __ có cần thiết trong Python không?

__Init__ trong Python là gì?Chất xây dựng __init__ mặc định trong C ++ và Java.Các hàm tạo được sử dụng để khởi tạo trạng thái của đối tượng.Nhiệm vụ của các hàm tạo là khởi tạo (gán giá trị) cho các thành viên dữ liệu của lớp khi một đối tượng của lớp được tạo.

__ init __ là gì (bản thân trong Python?

Bản thân trong từ khóa trong Python được sử dụng cho tất cả các trường hợp trong một lớp.Bằng cách sử dụng từ khóa tự, người ta có thể dễ dàng truy cập tất cả các trường hợp được xác định trong một lớp, bao gồm các phương thức và thuộc tính của nó.trong đó.__init__ là một trong những phương pháp dành riêng trong Python.Trong lập trình định hướng đối tượng, nó được gọi là một hàm tạo.used to all the instances in a class. By using the self keyword, one can easily access all the instances defined within a class, including its methods and attributes. init. __init__ is one of the reserved methods in Python. In object oriented programming, it is known as a constructor.