News from this site

 Rental advertising space, please contact the webmaster if you need cooperation


+focus
focused

classification  

no classification

tag  

no tag

date  

no datas

[Knowledge points] Mutual calls of classes in python

posted on 2023-05-21 16:36     read(1061)     comment(0)     like(19)     collect(1)


In Python, classes can call each other. This means that a class can access methods or properties of another class. Here are a few examples:

  1. Invocation of a class inside a module

In the same file, examples of mutual calls of different classes are as follows:

  1. the code

  1. class Car:
  2. def __init__(self, make, model):
  3. self.make = make
  4. self.model = model
  5. class Dealership:
  6. def __init__(self, name):
  7. self.name = name
  8. self.cars = []
  9. def add_car(self, make, model):
  10. new_car = Car(make, model)
  11. self.cars.append(new_car)
  12. def show_inventory(self):
  13. for car in self.cars:
  14. print(car.make, car.model)
  15. dealership = Dealership("ABC Motors")
  16. dealership.add_car("Toyota", "Camry")
  17. dealership.add_car("Honda", "Civic")
  18. dealership.show_inventory()

In the code above, the Dealership class calls the Car class. It adds the car by creating a Car object and displays the car's catalog by looping through the list of cars. This example shows how to call a class from another class in Python.

  1. operation result

  1. Toyota Camry
  2. Honda Civic
  1. Cross-module class calls

Mutual calls across file classes refer to mutual calls between two or more classes defined in different .py files.

  1. step

To realize mutual calls across file classes, the following steps need to be followed:

  • The classes that need to be called are defined in a separate .py file, which is called a module.

  • When using the class defined in this module in the current file, you need to use the module name plus a dot for reference. For example, if a class MyClass is defined in the module.py file, then when using this class in another file, you can write: module.MyClass() .

Practical application scenario: If there are many classes in your project, and each class has many methods, then you can put each class in a separate module to reduce code redundancy and make the code structure more Clear and easy to understand.

  1. the code

file 1 (module1.py)

  1. class ClassA:
  2. def __init__(self):
  3. self.name = 'ClassA'
  4. def greet(self):
  5. return 'Hello from ' + self.name

File 2 (module2.py):

  1. from module1 import ClassA
  2. class ClassB:
  3. def __init__(self):
  4. self.name = 'ClassB'
  5. def greet(self):
  6. return 'Hello from ' + self.name
  7. def call_class_a(self):
  8. a = ClassA()
  9. return a.greet()

In the main file of the program, these two classes can be used in the following way:

  1. from module2 import ClassB
  2. b = ClassB()
  3. print(b.greet())
  4. print(b.call_class_a())

As can be seen from the above code, the ClassB object obtains the instance of ClassA by calling the call_class_a method, and calls its greet method. This is a simple example of mutual calls across file classes.

  1. operation result

  1. Hello from ClassB
  2. Hello from ClassA



Category of website: technical article > Blog

Author:Disheartened

link:http://www.pythonblackhole.com/blog/article/25279/69b322fcb8c81775cf59/

source:python black hole net

Please indicate the source for any form of reprinting. If any infringement is discovered, it will be held legally responsible.

19 0
collect article
collected

Comment content: (supports up to 255 characters)