python的新手,我正在尝试创建一个小函数,用于将给定数量的字符串实例写入文件(例如,作为文字云工具的输入)。我试过用下面的类做这个,但由于某种原因没有收到任何输出,但也没有错误信息。
我想也许我没有正确地将count
声明为整数作为输入?但是,不会显示任何错误消息,这会使其有些混乱。
再次,非常新的Python所以任何帮助以及一些解释将非常感谢:)代码如下!
#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")
答案 0 :(得分: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
答案 1 :(得分:1)
首先,我确实遇到了语法错误......
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)
说明:
使wordlist成为一个类属性允许您将一次构建列表保留在类中,并且可以通过其他类方法轻松访问它,例如write_file
。
由于这一点,self.count
从调用__init__
的那一刻起保持不变。
简单地对课程进行实例化不会调用我们已经定义的所有方法。这意味着,除tool = print_list(10, "word")
之外,您还必须调用每种方法。顺便说一下," Python"是一个糟糕的名字...
一般评论:
你似乎对什么应该是一个类属性(例如self.x
而不是x
)感到困惑,应该是一个局部变量。以及如何使用这些。默认情况下,Python会查找局部变量但不是类属性。如果您要访问类属性,则必须使用self.
为其名称添加前缀。否则,您将获得NameError
或只是错误的结果。
答案 2 :(得分:1)
你有很多语法错误。首先,self
需要先行,类型转换不会发生在函数定义中。所以你的__init__
应该是
def __init__(self, count, word):
self.count = int(count)
self.word = word
其次,所有属性,例如wordlist
count
和word
都需要在方法中以self.wordlist
,self.word
等方式进行访问。例如,write_file
应为
def write_file(self):
my_file = open("cloud.txt", "w")
for word in self.wordlist:
my_file.write(word + "\n")
my_file.close
write_list
应该是
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
(我也没有缩进return语句,所以循环实际执行,但我认为这是复制到stackexchange错误。)
最后,你没有调用任何方法来填充wordlist
并编写它。因此,要让您的类实际编写文件,您需要调用write_file
方法。对代码进行这些更改后,我们有:
#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()