如何避免实例和类中的“缺少参数" [英] How do I avoid 'missing arguments' in instances and classes
问题描述
我目前正在为学校写一本应分为3个班级的:学生,老师,学校.(所有这些文件都在自己的.py文档中).
I am currently writing a for school that should have 3 classes: Student, Teacher, School. (all of them in their own .py document).
老师"课程拥有一个名称,主题和年龄的列表
The class "Teacher" holds a List of Name, subjects and Age
学生"类拥有姓名,课程和学生ID的列表
The class ''Student'' holds a List of Name, classes and student_IDs
"School"类是将两者结合在一起的类,它包含它们.
The class "School'' is the class that brings them both together, it contains them.
现在,如果我调用实例方法Student.print_list(),它将告诉我缺少一个参数.
Now if I call the instance method Student.print_list() it tells me I am lacking one Parameter.
自我是做什么的,为什么它是强制性的,如何避免不得不输入其他类的参数?
What does the self do, why is it mandatory, how do I avoid having to enter parameters from other classes?
我的第二个但同样的问题是,当我想打印出一个教师信息但必须发送一个参数时.显然,自我仍然不起作用/它接受了我给它的第一个参数.
My second but same problem is when I want to print out one Teacher info, but have to send one parameter. Obviously then the self still does not work/it takes the first parameter I give it.
我已经尝试了很多方法,但到目前为止都无济于事.我试过了:类方法(作弊),离开自我(错误),传递诸如list_students(False)(作弊)之类的东西.我如何合法地解决这个问题?
I have tried a bunch of things, but to no avail so far. I have tried: Class methods(cheating), leaving the self(wrong), passing anything like list_students(False)(cheating). How do I legitematly solve this problem?
student.py
student.py
class Student:
def __init__(self, name, student_id, classes):
self.name = name
self.student_id = student_id
self.classes = classes
student_list = []
student_list.append(Student('Nemo the Fish', '12-b-018241', 'english, maths, IT')
#a list of all student names etc. is entered here
school.py
school.py
import Student
class School:
def print_list(self):
for Student.Student in Student.student_list:
print Student.Student.name
main.py
import school
print_list()
显然,我想要一堆名字的列表,但是我得到了缺少1个必需的位置参数:自我".我觉得我做错了,是错误地调用了该方法,因为必须有一种方法
Obviously I would want a list of a bunch of names, but I get a 'missing 1 required positional argument: self'. I feel like what I did wrong is calling the method wrongly, because there must be a way to
(自我)像应该的那样存在
have the (self) be there like its supposed to be
在不提供垃圾参数的情况下调用该方法
call the method without giving trash parameters
不要使用任何@classmethod之类的东西
not use any @classmethod thing or anything alike
推荐答案
如果我正确理解了您提供的代码,则以下操作将满足您的要求:
The following would do what you want if I understood your provided code correctly:
student.py:
student.py:
class Student:
def __init__(self, name, student_id, classes):
self.name = name
self.student_id = student_id
self.classes = classes
school.py:
school.py:
from student import Student
class School:
def __init__(self):
self.student_list = []
self.student_list.append(Student('Nemo the Fish', '12-b-018241', 'english, maths, IT'))
def print_list(self):
for student in self.student_list:
print(student.name)
main.py:
from school import School()
this_school = School()
this_school.print_list()
尽管这不是将学生列表硬编码到班级定义中的最佳方法.
Although this is not the best approach to hardcode the list of students into the class definition.
这篇关于如何避免实例和类中的“缺少参数"的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!