0

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?

Leem.fin
  • 40,781
  • 83
  • 202
  • 354
  • why downvote my question? Please give a reason, I don't understand, I probably asked stupid question, but I am pretty new in Python. – Leem.fin Feb 22 '16 at 14:24
  • 1
    Define class variables to be shared by all methods in the class in the init method. I recommend finding a basic tutorial on python classes – Dan Feb 22 '16 at 14:26
  • @Dan, thanks, do you mean I should put `courses` list in init method? – Leem.fin Feb 22 '16 at 14:27
  • yes just like the top-voted answer right now – Dan Feb 22 '16 at 14:35

2 Answers2

5

Currently you have added courses as class-level attribute, but trying to access it as instance attribute, so you have to move courses initialization into __init__ method as in code snippet below:

def __init__(self):
    self.courses = ["Math", "Physics", "Chemical"]

Also you have to modify add_course method to append item into self.courses and not to self.course:

def add_course(self, course):
        if course not in self.courses:
           self.courses.append(course)

See SO Python: Difference between class and instance attributes question for more explanations of how and when each type of attributes shall be used.

Community
  • 1
  • 1
Andriy Ivaneyko
  • 20,639
  • 6
  • 60
  • 82
  • Thank you very much for pointing out my problem. I appreciate it more than just downvote which left me like helpless seeing those negative votings. – Leem.fin Feb 22 '16 at 14:28
  • @Leem.fin You are welcome! I hope answer would point you into right direction ) Good luck. – Andriy Ivaneyko Feb 22 '16 at 14:31
0

I found few mistakes in your code snippet, As you are new to Python, So thought of posting working code-

class School:
    courses=["Math","Physics","Chemical"]

    def __init__(self):
        return

    def add_course(self, course):
        if course not in self.courses:
           self.courses.append(course)

    def write_to_file(self):
        with open('courses.csv', 'w+') as course_file:
        wr = csv.writer(course_file, delimiter='\t')
        wr.writerow(self.courses)

My changes-

1. def write_to_file(self)
2.if course not in self.courses:
               self.courses.append(course)
3. wr.writerow(self.courses)
AlokThakur
  • 3,599
  • 1
  • 19
  • 32