如何使用导入方式将当前对象状态获取到python中的其他模块

问题描述 投票:0回答:1
class Catalog:
    def __init__(self):
        self.different_book_count = 0
        self.books = []

def accessCurrentCatalog(self):
        return self

class Library:
    def __init__(self,name, location, age, aadhar_id,emp_id):
        super().__init__(name, location, age, aadhar_id)
        self.emp_id = emp_id

    def __repr__(self):
        return self.name + self.location + self.emp_id


    def accesCurrentCatalog(self):   
        catalog = Catalog().accessCurrentCatalog
        return catalog


    def addBook(self,name,author,publish_date,pages):
        catalog = self.accesCurrentCatalog()
        catalog.addBook(name,author,publish_date,pages)

我有两个类,即目录类和库类,我已经创建了目录类的对象,并在目录类方法中进行了一些操作,我想将同一个对象与图书馆员类中进行的所有操作一起使用,以执行进一步的操作同时保持这些状态我尝试了上述方法,但没有成功以上两个类都在不同的模块或文件中......请帮我解决这个谢谢...

python-3.x oop project
1个回答
0
投票
我认为您正在寻找的是所谓的继承。假设您的Catalog类位于名为catalog.py的文件中,看起来像这样。
© www.soinside.com 2019 - 2024. All rights reserved.