尝试使用从第二个类中的该类调用另一个函数的类函数时,获取“ AttributeError”

时间:2019-07-04 04:39:10

标签: python function object

我正在编写一个小程序,该程序使用泡菜使人们在本地将时间表/工资表保存到计算机中。

我有以下课程:

class ScheduleDataService:

    def create_object_path(self, active_user):

        users_object_path = root / "user_objects" / active_user
        return users_object_path

    def save_users_schedule(self, users_schedule, users_object_path, active_user):
        schedule = open(users_object_path, "wb")
        pickle.dump(users_schedule, schedule)
        schedule.close()

        logger.debug("\nYour schedule has been successfully saved \n")
        logger.info(active_user + "has successfully saved their schedule.")

    def load_users_schedule(self, active_user):
        users_object_path = self.create_object_path(active_user)
        schedule = open(users_object_path, "rb")
        users_schedule = pickle.load(schedule)
        schedule.close()

        return users_schedule

在此类内,我在类函数“ load_users_schedule”内调用类函数“ create_object_path”。

在同一个模块中,我有第二个类,用于查看用户当前的时间表:

class ViewSchedule:

    __schedule_data_service: ScheduleDataService

    def __init__(self, schedule_data_service):
        self.__schedule_data_service = schedule_data_service

    def view_day(self, day_to_see, active_user):
        users_schedule = self.__schedule_data_service.load_users_schedule(self, active_user)


        to_view = []

        if day_to_see == "1":
            to_view.append("Monday")
        elif day_to_see == "2":
            to_view.append("Tuesday")
        elif day_to_see == "3":
            to_view.append("Wednesday")
        elif day_to_see == "4":
            to_view.append("Thursday")
        elif day_to_see == "5":
            to_view.append("Friday")
        elif day_to_see.lower() == "all":
            to_view = ["Monday", "Tuesday", "Wednesday", "Thursday", "Friday"]
        else:
            logger.debug("Sorry that isn't a valid option please try again")
            logger.info(active_user + "failed to view " + day_to_see + " from their schedule due to incorrect input")
            pass

        for day in users_schedule.week:
            if day.name in to_view:
                logger.debug("")
                for session in day.sessions:
                    logger.debug("Day: " + day.name + " Class Code: " + session.code + " Class length: "
                                 + session.length)
                logger.debug("")

我试图给这个类是上面类的自己的实例。然后,我试图调用“ load_users_schedule”函数来加载用户时间表。当我这样做时,出现以下错误。

File "generator.py", line 116, in <module>
    ViewSchedule(ScheduleDataService).view_day(day_to_see, active_user)
  File "/shared/projects/pay_sheet_generator/schedule_data.py", line 249, in view_day
    users_schedule = self.__schedule_data_service.load_users_schedule(self, active_user)
  File "/shared/projects/pay_sheet_generator/schedule_data.py", line 40, in load_users_schedule
    users_object_path = self.create_object_path(active_user)
AttributeError: 'ViewSchedule' object has no attribute 'create_object_path'

当我从“ load_users_schedule”中删除“ create_object_path”并独立运行它们时,代码可以正常工作。我正在寻找的是一种将函数彼此嵌入的方法,或者是为什么不应该这样做的解释。

编辑:

对ViewSchedule的调用如下

ViewSchedule(ScheduleDataService).view_day(day_to_see, active_user)

2 个答案:

答案 0 :(得分:1)

您的代码中有一些问题。

  1. 在行users_schedule = self.__schedule_data_service.load_users_schedule(self, active_user)上,不需要添加self作为第一个参数,因为当您使用实例调用类的方法时,会自动插入第一个参数该类的实例

  2. 您需要将ScheduleDataService的实例传递给ViewSchedule的实例,因此您需要执行ViewSchedule(ScheduleDataService()).view_day(day_to_see, active_user)而不是ViewSchedule(ScheduleDataService).view_day(day_to_see, active_user)

  3. 另外,您应该知道dunder变量在python中是name-mangled,因此请在您的类中谨慎使用它们

答案 1 :(得分:1)

这有点满足您的需求。如果人们可以复制并运行您的代码,这将很有帮助。您有一些变量和导入未包含在代码中。

import pickle
import logging

root = ""
logger = logging.getLogger()


class ScheduleDataService:

    def create_object_path(self, active_user):

        users_object_path = root + "user_objects" + active_user
        return users_object_path

    def save_users_schedule(self, users_schedule, users_object_path, active_user):
        schedule = open(users_object_path, "wb")
        pickle.dump(users_schedule, schedule)
        schedule.close()

        logger.debug("\nYour schedule has been successfully saved \n")
        logger.info(active_user + "has successfully saved their schedule.")

    def load_users_schedule(self, active_user):
        users_object_path = self.create_object_path(active_user)
        schedule = open(users_object_path, "rb")
        users_schedule = pickle.load(schedule)
        schedule.close()

        return users_schedule


class ViewSchedule:

    # This gets automatically created inside __init__       __schedule_data_service: ScheduleDataService

    def __init__(self, schedule_data_service):
        self.__schedule_data_service = schedule_data_service

    def view_day(self, day_to_see, active_user):
        users_schedule = self.__schedule_data_service.load_users_schedule(active_user)


        to_view = []

        if day_to_see == "1":
            to_view.append("Monday")
        elif day_to_see == "2":
            to_view.append("Tuesday")
        elif day_to_see == "3":
            to_view.append("Wednesday")
        elif day_to_see == "4":
            to_view.append("Thursday")
        elif day_to_see == "5":
            to_view.append("Friday")
        elif day_to_see.lower() == "all":
            to_view = ["Monday", "Tuesday", "Wednesday", "Thursday", "Friday"]
        else:
            logger.debug("Sorry that isn't a valid option please try again")
            logger.info(active_user + "failed to view " + day_to_see + " from their schedule due to incorrect input")
            pass

        for day in users_schedule.week:
            if day.name in to_view:
                logger.debug("")
                for session in day.sessions:
                    logger.debug("Day: " + day.name + " Class Code: " + session.code + " Class length: "
                             + session.length)
                logger.debug("")


active_user = "Bob"
day_to_see = 1

scheduleDataSvc = ScheduleDataService()
vs = ViewSchedule(scheduleDataSvc)

vs.view_day(day_to_see, active_user)