首页
学习
活动
专区
圈层
工具
发布
社区首页 >问答首页 >Python类:'int‘对象在my类中的函数中没有属性

Python类:'int‘对象在my类中的函数中没有属性
EN

Stack Overflow用户
提问于 2020-01-07 09:05:41
回答 2查看 1.6K关注 0票数 1

我是Python面向对象编程的新手。我在网上复制了这段代码,以便更好地了解OOP在Python中的工作方式。以下是代码:

代码语言:javascript
复制
class Student:

    def __init__(self, name, student_number):
        ## Student has-a name
        self.name = name
        ## Student has-a student number
        self.student_number = student_number
        ## Student has-many classes
        self.classes = []

    def enrol(self, course_running):
        ## Append the list, classes with variable course_running
        self.classes.append(course_running)
        ## Call course_running with the add_student function in a class
        course_running.add_student(self)

class Department:

    def __init__(self, name, department_code):
        ## Department has-a name
        self.name = name
        ## Department has-a code
        self.department_code = department_code
        ## Department has-many courses
        self.courses = {}

    def add_course(self, description, course_code, credits):
        self.courses[course_code] = Course(self, description, course_code, credits)
        return self.courses[course_code]

class Course:

    def __init__(self, description, course_code, credits, department):
        ## Course has-a description
        self.description = description
        ## Course has-a code
        self.course_code = course_code
        ## Course has-many credits
        self.credits = credits
        ## Course has-a deparmtnet
        self.department = department
        ## Adds the aforementioned course to the necessary DPT; it is usefull because
        ## it automatically adds this course to the DPT given in its parameter
        self.department.add_course(self)
        ## Course has-many runnings
        self.runnings = []

        def add_running(self, year):
            self.runnings.append(CourseRunning(self, year))
            return self.runnings[-1]

class CourseRunning:

    def __init__(self, course, year):
        ## CourseRunning has-a year
        self.course = course
        ## CourseRunning has-a year
        self.year = year
        ## CourseRunning has-many students
        self.students = []

    def add_student(self, student):
        self.students.append(student)

maths_dept = Department("Mathematics and Applied Mathemtics", "MAM")
mam1000w = maths_dept.add_course("Mathematics 1000", "MAM1000W", 1)
mam1000w_2013 = mam1000w.add_running(2013)

bob = Student("Bob", "Smith")
bob.enrol(mam1000w_2013)

然而,我不断地发现这些错误:

代码语言:javascript
复制
Traceback (most recent call last):
  File "ex42c.py", line 70, in <module>
    mam1000w = maths_dept.add_course("Mathematics 1000", "MAM1000W", 1)
  File "ex42c.py", line 30, in add_course
    self.courses[course_code] = Course(self, description, course_code, credits)
  File "ex42c.py", line 47, in __init__
    self.department.add_course(self)
AttributeError: 'int' object has no attribute 'add_course'

我将'int‘对象更改为'list’对象,然后改为'str‘对象,但它们产生了类似的错误。当我删除整数和所有‘信用’变量时,'course_code‘变量和str“MAM1000W”变量发生了相同的错误。基本上,我想了解为什么不能将这些参数传递给函数。

提前谢谢。

EN

回答 2

Stack Overflow用户

发布于 2020-01-07 09:27:13

删除上述内容( self.department.add_course(self))后,请取消以下部分:

代码语言:javascript
复制
def add_running(self, year):
    self.runnings.append(CourseRunning(self, year))
    return self.runnings[-1]

它对我有效,虽然我不确定这是否是你希望它工作的方式。

它以前不起作用,因为您试图在课程中使用Department的方法。部门只是一个Integer类型的论点,而不是您可能想要的department对象。同样的方法已经存在于Department类中了,为什么不只使用它呢?

第二部分是取消缩进add_running()函数,因为它是在init块中输入的。

我希望这能帮上忙。

票数 0
EN

Stack Overflow用户

发布于 2020-01-07 10:12:55

第一个问题是,您将参数以错误的顺序传递给Course

而不是self.courses[course_code] = Course(self, description, course_code, credits)

你需要self.courses[course_code] = Course(description, course_code, credits, self)

因为在您的Course.__init__中,它们按顺序排列:def __init__(self, description, course_code, credits, department)

,然后是,您有一个递归循环,其中add_course函数由Course __init__调用,在add_course中创建Course时调用.以此类推。

除了也不会发生这种情况,因为调用Course.__init__中的self.department.add_course(self)缺少了Department.add_course - course_codecredits所需的两个参数。

简而言之:你在这里有很多问题。我们可以修复其中的大多数,但是关于递归调用add_course的决定将要求您决定是希望部门添加课程,还是让课程将自己添加到部门中。

票数 0
EN
页面原文内容由Stack Overflow提供。腾讯云小微IT领域专用引擎提供翻译支持
原文链接:

https://stackoverflow.com/questions/59625167

复制
相关文章

相似问题

领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档