python包中的循环依赖

我的任务是制作一个名为“ project”的python包。在内部,我必须创建2个具有我事先指定的名称的单独文件。在每一个内部,我必须放置1个带有其参数和方法的类。到目前为止一切都很好。但这是问题所在-每个类都引用另一个类以使其正常工作(在任务描述中,为我提供了两个类的方法的名称,其中一些必须引用另一个类的对象,所以我当然使用了project.xxxx导入Xxxx)。

我有带方法的类库-

add_user(用户:用户)

我有方法的类User

get_book(作者:str,book_name:str,days_to_return:int,库:Library)

在这里,我了解了循环依赖问题。我为一些文章提供了解决方法-将这些内容合并到一个文件中或制作更多文件并重组代码。那是不行的-名为“ project”的程序包必须有2个带有提供名称的文件,每个文件中都有1个类,因为我必须将带有该程序包的zip文件提交到在线自动化系统进行测试。

我遇到的另一件事是将import语句放在一个文件的末尾。我试过两个文件上的1个文件,但仍然出现ImportError。

将非常感谢您的帮助。

这是头等舱:

from project.user import User

class Library:
    def __init__(self):
        self.user_records = []
        self.books_available = {}
        self.rented_books = {}

    def add_user(self, user: User):
        if user.username in [u.username for u in self.user_records]:
            return f'User with id = {user.id} already registered in the library!'
        self.user_records.append(user)

    def remove_user(self, user: User):
        if user.username not in [u.username for u in self.user_records]:
            return 'We could not find such user to remove!'
        for i, o in enumerate(self.user_records):
            if o.username == user.username:
                del self.user_records[i]

    def change_username(self, user_id: int, new_username: str):
        user = [u for u in self.user_records if u.id == user_id][0]
        if not user:
            return f'There is no user with id = {user_id}!'
        if user.username == new_username:
            return f'Please check again the provided username - it should be different than the username used so far!'
        user.username = new_username
        return f'Username successfully changed to: {new_username} for userid: {user_id}'

这是第二堂课:

from project.library import Library

class User:
    def __init__(self, user_id: int, username: str):
        self.books = []
        self.id = user_id
        self.username = username

    def get_book(self, author: str, book_name: str, days_to_return: int, library: Library):
        rented_books = [x for x in library.rented_books.values()]
        if rented_books:
            for x in rented_books:
                if book_name in x:
                    days = x[book_name]
                    return f'he book "{book_name}" is already rented and will be available in' \
                           f' {days} days!'
        if book_name in library.books_available[author]:
            if self.username not in library.rented_books:
                library.rented_books[self.username] = {}
            library.rented_books[self.username].update({book_name: days_to_return})
            library.books_available[author].remove(book_name)
            self.books.append(book_name)
            return f'{book_name} successfully rented for the next {days_to_return} days!'

    def return_book(self, author:str, book_name:str, library: Library):
        if book_name in self.books:
            library.books_available[author].append(book_name)
            library.rented_books[self.username].pop(book_name)
            self.books.remove(book_name)
        else:
            return f"{self.username} doesn't have this book in his/her records!"

    def info(self):
        books = sorted(self.books, key=lambda x: x)
        return ', '.join(books)
评论