I might ask a stupid question. I just started learning Python.
I have a simple python script, which defined a class with two methods: add_course
and write_to_file
:
class School:
courses=["Math","Physics","Chemical"]
def __init__(self):
return
def add_course(self, course):
if course not in self.course:
self.course.append(course)
def write_to_file():
course_file = open('courses.csv', 'w+')
wr = csv.writer(course_file, delimiter='\t')
writer.writerow(self.courses)
// create a School instance
school = School()
// add course
school.add_course("test")
school.add_course("test2")
school.add_course("test")
// write course to file
school.write_to_file()
I run the above script, then, I open the courses.csv file, I don't see "test" and "test2", but I can see the courses "Math","Physics","Chemical" Why?