![]() In the meantime, I know a couple of things that you could check. I'm sure someone who knows what they're doing will come along shortly. After you do this, the methods are exactly the same.īy defining a generic book list class, not only do you not have to keep redefining common functionality, you can also take advantage of polymorphic functions.Hi There are a lot of simmers with much more experience in this type of thing than I. Sliepen said, you should simplify the Library constructor to match the ReadingList constructor. def _init_(self, books = None):Īnd here are the analogous methods from the ReadingList class. Here are some methods from the Library class. Since Library and ReadingList are just different types of lists of books, I would opt for define a generic BookList as a base class from which Library and ReadingList both inherit.ĭoing this saves you from the repetition of having to define common methods like the following, which both of your classes already do. Whenever you call a member function that accesses the list of books on the reading list, this member function first has to filter out all the books that are no longer in the Library. There are two options:Ī Library keeps a list of ReadingLists that reference its Books, and when you call remove_book() on a Library, it will in turn iterate over all the ReadingLists and call remove_book() on them as well.Ī ReadingList keeps a reference to the Library whose Books it contains. However, if you want a book to disappear from the ReadingList when it is removed from the Library, then somehow there needs to be some communications between those two objects. Why would a book magically disappear from a real life reading list if your library removes that book? Note that your code is perfectly fine, Python won't actually delete the Book object from memory until the last reference to it is gone. Removing books from the LibraryĪs you noticed, you can remove a book from the library, but it won't be removed from any reading lists. Prefer using verbs for function names, and nouns for variables.Īnd I also wonder why a ComicBook has an artist, but a Novel has no author? And why is there a get_artist() but no get_title(), get_publication_date() and so on? Making everything consistent will make using your classes much easier. ![]() total_pages() should be renamed get_total_pages() or calculate_total_pages().pages_over_n() should be get_books_with_over_n_pages() or something similar, to indicate that it returns Books, not pages.get_library() should be get_books(), since it only returns the list of books, not an object of type Library.I would name it books in both cases.Īlso be consistent with the names of functions: In class Library, the list of books is called books, but in class ReadlingList you named it list. That is a bit surprising.Īlso be consistent with naming things. You take an optional list of books in the constructor of class Library, but there is no such thing in class ReadingList. Note that to prevent the issue of mutable default arguments as mentioned by ojdo, you should make a copy of the initial list. ![]() Why not use as the default value? class Library: You have a default value of None for the parameter books, but then immediately proceed to convert that None to an empty list. Simplify the constructor of class Library My_lib.new_book(Novel("Aryan Parekh","Stack")) My_lib.new_book(Novel("J.K Rowling","Harry Potter")) # Comic book stuff which isn't common in all books # Novel stuff which isn't common in all books Return ĭef _init_(self, title, pages, publicationDate): """Returns books that have more than specified number of pages""" I don't know if there's a way to deal with this, or if I'm thinking about this the wrong way. Also there is an issue whereby if you remove a book from the library, it will still show up in any reading lists I created. Speaking of structure, I would just like to know if mine is any good. But hopefully from the below you get a good idea of the structure. Note that I've removed quite a lot from this, just so it's not 5 pages long! I would normally have several different types of books, attributes such as Author etc. For example, a library method which will return all books over a specified length. Program allows one to add books (comics or novels - with comics inheriting from novels) to one's own library, and create a custom reading lists. However I thought I'd put together a personal online book library to practice inheritance, methods, docstrings etc. Very new to OOP with Python - and feel like I still don't totally get OOP.
0 Comments
Leave a Reply. |