0

Quite new to python, I am trying to create a small function that is meant to write a given number of instances of a string to a file (e.g. as input for a word cloud tool). I have tried doing this with the class below but for some reason do not receive any output, but no error message either.

I thought maybe I am not declaring count correctly as an integer as input? However, no error message is displayed which makes it somewhat confusing.

Again, very new to Python so any help along with some explanation would be greatly appreciated :) Code below!

#Prints multiple instances of a string as input for a word cloud tool


class print_list(object):
    wordlist = []
    def __init__(int(count), word, self):
        self.count = count
        self.word = word

    def write_list(self):
        while count > 0:
            wordlist.append(word)
            print word + "\n"
            count = count - 1
            return wordlist

    def write_file(self):
        my_file = open("cloud.txt", "w")
        for word in wordlist:
            my_file.write(word + "\n")
        my_file.close        


Python = print_list(10, "Python")

3 Answers 3

1

You have a lot of syntax errors. First of all, self needs to come first, and type conversions don't happen in function definitions. so your __init__ should look like

def __init__(self, count, word):
        self.count = int(count)
        self.word = word

Second, all attribute, like wordlist count and word need to be accessed as self.wordlist, self.word etc. inside the methods. So for example, write_file should be

def write_file(self):
    my_file = open("cloud.txt", "w")
    for word in self.wordlist:
        my_file.write(word + "\n")
    my_file.close

And write_list should be

def write_list(self):
    while self.count > 0:
        self.wordlist.append(self.word)
        print self.word + "\n"
        self.count = self.count - 1
    return self.wordlist

(I also un-indented the return statement so the loop actually gets executed, but I assume that was a copying into stackexchange error).

Lastly, you are not calling any of your methods that do the things like filling in wordlist and writing it. So to get your class to actually write the file you need to call the write_file method. Making these changes to your code, we have:

#Prints multiple instances of a string as input for a word cloud tool


class print_list(object):
    wordlist = []
    def __init__(self, count, word):
        self.count = count
        self.word = word

    def write_list(self):
        while self.count > 0:
            self.wordlist.append(self.word)
            print self.word + "\n"
            self.count = self.count - 1
        return self.wordlist

    def write_file(self):
        my_file = open("cloud.txt", "w")
        for word in self.wordlist:
            my_file.write(word + "\n")
        my_file.close()


Python = print_list(10, "Python")
Python.write_list()
Python.write_file()
Sign up to request clarification or add additional context in comments.

Comments

1

First of all, I did get a syntax error...

class print_list(object):
    #int(count) isn't necessary and causes Syntax Error on my python 2.7
    def __init__(self, count, word): 
        self.count = count
        self.word = word
        #I think wordlist will be more useful here, as a class attribute (1)
        self.wordlist = []

    def write_list(self):
        #I copy the self.count to a local variable (2)
        countCopy = self.count
        while count > 0:
            self.wordlist.append(self.word)
            print self.word + "\n"
            countCopy = countCopy - 1

    def write_file(self):
        #nitpick: hardcoding filenames is bad practice, it would be better to pass it as an argument
        my_file = open("cloud.txt", "w")
        #self.wordlist is the list built in the above function
        #so if this function is called first, self.wordlist is an empty list
        for word in self.wordlist:
            my_file.write(word + "\n")
        #you forgot parentheses below
        my_file.close()

#More code here... (3)

Remarks:

  1. Making wordlist a class attribute allows you to keep the once build list inside the class, and it is easily accessed by other class methods, like write_file.

  2. Thanks to this, the self.count remains unchanged since the moment __init__ was called.

  3. Simply instancing the class won't call all the methods we've defined. This means, that aside from tool = print_list(10, "word") you'll have to call each method as well. By the way, "Python" is a bad name for a class...

And a general comment:

You seem to get confused with what should be a class attribute (e. i. self.x instead of x) and what should be a local variable. And how to use those. Python, by default, looks for local variables but not class attributes. If you want to access a class attribute you have to prefix its name with self.. Otherwise you'll get a NameError or simply a wrong result.

1 Comment

Thanks, your solution is great and your comments are really helpful; many lessons learned from this one!
1
class PrintList(object):
    def __init__(self,count, word):# self comes first in your init method
        self.count = count
        self.word = word # use self to refer to instance attributes
        self.wordlist=[]  # make wordlist an attribute


    def write_list(self):
        while self.count > 0:
            self.wordlist.append(self.word)
            print self.word + "\n"
            self.count -= 1
        return self.wordlist

    def write_file(self):
        with  open("cloud.txt", "w") as my_file: # using with automatically closes the file
            for word in self.wordlist:
                my_file.write(word + "\n")


pl = PrintList(10, "Python") # create instance of Printlist class
print pl.write_list() 
pl.write_file()# need to call method on instance to write to the file

Python classes

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.