我需要将StudentCourse类中的getSelectedCourses方法中的selectedCourses传递给Advisor类方法ApproveSelectedCourse。实际上,我实际上需要使用Student类中的sendCourseRegistrationRequest方法,以将getSelectedCourses方法中的selectedCourses列表传递给Advisor类方法ApproveSelectedCourse,但是我不知道如何将列表从一个类传递给另一个类。或者甚至如何将其传递给Student类中的单独方法。在这一点上,我也没有想法,也没有时间。这是我最后的镜头。感谢您的帮助!
我尝试将Student类作为参数添加到Advisor类,并尝试在所有类之外定义变量以引用我要使用的Student类的方法。我尝试过将list变量传递给Student类中的单独方法。我尝试过将Student方法设置为类方法和静态方法,但是我什至不确定@classmethod和@staticmethod的工作方式或如何使用它们来帮助我。至此,我已经尝试了所有我能想到的。
import random
class Person:
def __init__(self, name):
self.name = name
def getName(self):
print('Student name is: {}'.format(self.name))
class Student(Person):
def __init__(self, name, stuID):
#print('I am a student')
Person.__init__(self, name)
self.stuID = stuID
self.finishedCourses = []
self.selectedCourses = []
def getStuID(self):
print('Student ID is: {}'.format(self.stuID))
def getSelectedCourse(self, courseList):
SelectedCourse = []
for i in courseList:
if i < 400:
SelectedCourse.append(i)
#print(SelectedCourse)
for key in prerequisiteDict.keys():
if key in SelectedCourse:
SelectedCourse.remove(key)
#print(SelectedCourse)
i = 0
while i <= 2:
finishedCourses = random.choice(SelectedCourse)
self.finishedCourses.append(finishedCourses)
#print(finishedCourses)
i += 1
print('Finished courses: {}'.format(self.finishedCourses))
x = 0
while x <= 2:
selectedCourses = random.choice(courseList)
self.selectedCourses.append(selectedCourses)
x += 1
print('Selected courses: {}'.format(self.selectedCourses))
def sendCourseRegistrationRequest(self):
pass
## for key in prerequisiteDict.keys():
## if key not in self.finishedCourses:
##
## self.finishedCourses.remove(key)
## print('Update selected courses: {}'.format(self.finishedCourses))
def updateSelectedCourse():
pass
class Advisor(Person):
def __init__(self, name):
print("I'm here.")
Person.__init__(self, name)
def approveSelectedCourse(self, Student):
print("I'm here2.")
## Student.__init__(self, finishedCourses)
## self.var1 = Student.finishedCourses
## print('self.var1 is {}: '.format(self.var1))
##
##
## for key in prerequisiteDict.keys():
##
## if key in SelectedCourse:
## return True
## else:
## self.var1.remove(key)
## print('updated course selection: {}'.format(self.selfvar1))
##
##
## for key in prerequisiteDict.keys():
## if key in getSelectedCourse:
##
## getSelectedCourse.remove(key)
## print(getSelectedCourse)
courseList = [110, 213, 315, 316, 412, 121, 223, 326, 328, 422,
136, 238, 335, 336, 432, 140, 243, 345, 346, 448,
150, 253, 355, 356, 452]
prerequisiteDict = {213:110, 412:316, 326:223, 422:328,
238:136, 345:243, 355:253, 452:356}
getSelectedCourse = Student.getSelectedCourse
stuObj = Student('Alice', 98980)
stuObj.getName()
stuObj.getStuID()
stuObj.getSelectedCourse(courseList)
#stuObj.sendCourseRegistrationRequest()
##AdvObj = Advisor('Cindy')
##AdvObj.approveSelectedCourse('Alice')
基本上,我的代码以我想象的方式通过Student getSelectedCourses方法工作。在我看来,解决我的问题的最简单方法是将self.selectedCourses = []发送到sendCourseRegistrationRequest Student方法,然后将其传递给Advisor方法approveSelectedCourse,然后该方法将检查selectedCourses列表以及是否从prereq存在于selectedCourses中,它将从selectedCourses列表中删除该课程。理想情况下,我只是想找出一种方法,将selectedCourses列表从Student类传递到Advisor类,但是我不知道如何。
答案 0 :(得分:1)
我认为这里的问题是组织。由于Advisor
继承自Person
,因此没有self.selectedCourses
属性。我要做的是以下两件事之一:
Advisor_instance.advisees
属性中的字典,以便您可以按名称查找class Advisor(Person):
def __init__(self, name, advisee=None):
print("I'm here.")
Person.__init__(self, name)
self.advisees = {advisee.name: advisee} if advisee else {}
def add_advisee(self, student_inst):
if student_inst.name in self.advisees:
raise KeyError("Entry already exists")
self.advisees[student_inst.name] = student_inst
# Easy name lookup and returns the list
def get_course_list(self, name):
# Will raise keyerror if name not in advisees
return self.advisees.[name].selectedCourses
alice = Student("Alice", 98980)
cindy = Advisor("Cindy", alice)
cindy.get_course_list('alice')
就我个人而言,我认为第一个实现对您的用例而言更加直观。顾问可以提供许多建议,并且存储Student
的实例可以进行简单的名称查找,您无需传递任何内容。
将其更具体地扩展到您要寻找的内容:
class Advisor(Person):
def __init__(self, name, advisee=None):
print("I'm here.")
Person.__init__(self, name)
self.advisees = {advisee.name: advisee} if advisee else {}
def add_advisee(self, student_inst):
if student_inst.name in self.advisees:
raise KeyError("Entry already exists")
self.advisees[student_inst.name] = student_inst
def approveSelectedCourse(self, student_name):
# don't set a self attr here
# will still raise a KeyError for missing advisees
var1 = self.advisees[student_name].finishedCourses
# Do things with var1
class Advisor(Person):
def __init__(self, name):
print("I'm here.")
Person.__init__(self, name)
def get_course_list(self, student_inst):
return student_inst.selectedCourses
alice = Student("Alice", 98980)
cindy = Advisor("Cindy")
cindy.get_course_list(alice)
如果Advisor
的实例更通用,即不需要特定于 Advisor
的方式,则这种方式会更好地工作,只需要他们能够完成类似Advisor
的方法。在这种情况下,name
属性可能会丢失,方法可能是静态的,在这种情况下,为什么要使用类?