classBook: list_of_reviews=  def __init__(self,info_about_author,year_of_publication,genre,publisher): self.info_about_author= info_about_author self.year_of_publication= year_of_publication self.genre= genre self.publisher= publisher def __str__(self): author= 'Author: ' + self.info_about_author year= 'Year of publication: ' + self.year_of_publication genre= 'Genre: ' + self.genre publisher= 'Name of publisher: ' + self.publisher reviews= 'Reviews about book: ' + '\n' + '\n'.join(self.list_of_reviews) return author + '\n' + year + '\n' + genre + '\n' + publisher + '\n' + reviews def __repr__(self): return self.__str__() def __eq__(self,other): if str(self) is str(other): return True else: return False def __ne__(self,other): if repr(self) is not repr(other): return True else: return False class Book_review: def __init__(self,review): self.review= review def add_review(self,obj): #method of adding Book class object to review list obj.list_of_reviews.append(self.review) book1= Book('Pete Barker','2007','Historical genre','Hamish Hamilton') book2= Book('Aldous Huxley','1932','Science fiction','Harper Collins') review1= Book_review('Brave New World explores the negatives of a ostensibly successful world in which everyone appears to be content and satisfied, with excessive carnal pleasures yet really, this stability is only achieved by sacrificing freedom in its true sense and the idea of personal responsibility.') review2= Book_review('It is true that this book is a complex read and I must confess that some parts I did not understand; however, the novels meaning has left a deep impression on me. Its certainly a book I wont forget,and I would recommend it to readers aged fourteen and over as the ideas presented are complex, and Huxley writes in a very adult-like manner, with exceedingly complicated sentences and very complex vocabulary.') review1.add_review(book2) #should only add reviews to this object review2.add_review(book2) print(str(book1)) print(str(book2)) print(repr(book1)) print(repr(book2)) print(book1== book2) print(book2 != book2)
Answer # 1
Change the class like this:
classBook: def __init__(self, info_about_author, year_of_publication, genre, publisher): self.list_of_reviews=  self.info_about_author= info_about_author self.year_of_publication= year_of_publication self.genre= genre self.publisher= publisher ...
Now why did this happen. There is a class, and there is an object. For example you have a class
Book, but there are objects
The way you described the property:
classBook: list_of_reviews=  ...
Is the creation of a property on a class. And since class is common to all objects, then, accordingly, the property
list_of_reviewswas common to all books
This feature can be bypassed by changing the object reference of the class property of the object itself:
classBook: list_of_reviews= None def __init__(self, info_about_author, year_of_publication, genre, publisher): self.list_of_reviews=  ...
Then, each new object will have its own property.
UPD. There is a way to simplify the creation of a class with a constructor, an annotation of fields, a comparison mechanism, and a string description method for objects through dataclasses
For annotation from built-in types of type
list[str]you need python 3.10 and above, for earlier versions, it can be replaced with
Rewrote the code:
from dataclasses import dataclass, field @dataclass classBook: info_about_author:str year_of_publication: str genre: str publisher: str list_of_reviews: list[str]= field(default_factory=list, repr=False) def print_info(self): print(self) book1= Book('Pete Barker','2007','Historical genre','Hamish Hamilton') print(book1) # Book(info_about_author='Pete Barker', year_of_publication='2007', genre='Historical genre', publisher='Hamish Hamilton') book1.print_info() # Book(info_about_author='Pete Barker', year_of_publication='2007', genre='Historical genre', publisher='Hamish Hamilton')
- python : Bot on aiogram does not see the answer to someone else's message
- python : Input() constraint (int, float)
- Loading bar in Python 3.x
- Program for finding synonyms of Russian words python
- python : How to create a window in the game for example (dota2)
- python : How to speed up polynomial hash calculation
- python : Sending a file using telegramAPI
- Authorization key before running Python script