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?

4
  • why downvote my question? Please give a reason, I don't understand, I probably asked stupid question, but I am pretty new in Python. Commented Feb 22, 2016 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 Commented Feb 22, 2016 at 14:26
  • @Dan, thanks, do you mean I should put courses list in init method? Commented Feb 22, 2016 at 14:27
  • yes just like the top-voted answer right now Commented Feb 22, 2016 at 14:35

2 Answers 2

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.

Sign up to request clarification or add additional context in comments.

2 Comments

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 You are welcome! I hope answer would point you into right direction ) Good luck.
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)

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.