posted on 2023-05-21 16:36 read(1133) 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:
In the same file, examples of mutual calls of different classes are as follows:
- class Car:
- def __init__(self, make, model):
- self.make = make
- self.model = model
-
- class Dealership:
- def __init__(self, name):
- self.name = name
- self.cars = []
-
- def add_car(self, make, model):
- new_car = Car(make, model)
- self.cars.append(new_car)
-
- def show_inventory(self):
- for car in self.cars:
- print(car.make, car.model)
-
- dealership = Dealership("ABC Motors")
- dealership.add_car("Toyota", "Camry")
- dealership.add_car("Honda", "Civic")
- 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.
- Toyota Camry
- Honda Civic
Mutual calls across file classes refer to mutual calls between two or more classes defined in different .py files.
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.
In another file, use the import statement to import the 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.
file 1 (module1.py)
- class ClassA:
- def __init__(self):
- self.name = 'ClassA'
-
- def greet(self):
- return 'Hello from ' + self.name
File 2 (module2.py):
- from module1 import ClassA
-
- class ClassB:
- def __init__(self):
- self.name = 'ClassB'
-
- def greet(self):
- return 'Hello from ' + self.name
-
- def call_class_a(self):
- a = ClassA()
- return a.greet()
In the main file of the program, these two classes can be used in the following way:
- from module2 import ClassB
-
- b = ClassB()
- print(b.greet())
- 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.
- Hello from ClassB
- Hello from ClassA
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.
name:
Comment content: (supports up to 255 characters)
Copyright © 2018-2021 python black hole network All Rights Reserved All rights reserved, and all rights reserved.京ICP备18063182号-7
For complaints and reports, and advertising cooperation, please contact vgs_info@163.com or QQ3083709327
Disclaimer: All articles on the website are uploaded by users and are only for readers' learning and communication use, and commercial use is prohibited. If the article involves pornography, reactionary, infringement and other illegal information, please report it to us and we will delete it immediately after verification!