I have a class Book, and this basically only returns the content for now, but i have an external file that i need to read, and then pass the content into that instance, for instance i start declaring the book instance as b1
class Book():
def __init__(self,poem="empty"):
self.poem = poem
def __str__(self):
return self.poem
def reading(instance, file_content):
list_of_content = []
with open(file_content, "r") as f:
for i in f:
list_of_content.append(i.split())
flatten = [item for sublist in list_of_content for item in sublist]
string = " ".join(flatten)
instance = Book(string)
return instance
b1 = Book() # book has a default value so it wont make any error
reading(b1, "file.txt")
print("File contains:",b1) # prints empty, because reading function has not passed any data i think
The problem is that now it does print always only "empty", how can i pass the data that i read from the file to the instance that is called at reading(), this is for learning purpose.