How do I define a method to store a collection (e.g., dictionary)?

2024/11/24 17:01:44

I'm a beginner working on a library management system and there's something I can't get my head around.

So I have a Books class that creates new book records.

class Books:def __init__(self, title=None, author=None, year=None, publisher=None, num_copies=None,num_available_copies=None, publication_date=None):self.bookID = random.randint(1, 1000)self.title = titleself.author = authorself.year = yearself.publisher = publisherself.num_copies = num_copiesself.num_available_copies = num_available_copiesself.publication_date = publication_date

I then have to define another class 'BookList' which has the following requirement: 'Define a method to store a collection (e.g., dictionary). The collection should store book instances that are created from the Book object.' How do I do that?

Answer

Following your request exactly would result in this new class:

class BookList:def __init__(self):self.list_of_books = []def add_book(self, book: Books):self.list_of_books.append(book)

This is not exactly the best way to do python. But I guess anything goes in a learning scenario. For a slightly better implementation of all of this refer to the code below:

from dataclasses import dataclass@dataclass
class Book:book_id               : inttitle                 : strauthor                : stryear                  : intpublisher             : strnum_copies            : intnum_available_copies  : intpublication_date      : intclass BookList:def __init__(self):self._books = []@propertydef books(self):return self._books@books.setterdef books(self, book):if isinstance(book, Book):self._books.append(book)

now using it will be like this:

>>> book_list = BookList()
>>> book_list.books
[]
>>> book_list.add_book(Book(Bunch of parameters here))
>>> book_list.books
[Book(Parameters from the book you made)]

you can do a lot more than what I did, I added the properties so that if you decide to copy this code to pass some kind of school class, you'll have some explaining to do. Otherwise good luck!

https://en.xdnf.cn/q/120512.html

Related Q&A

Python Regex punctuation recognition

I am stumped by this one. I am just learning regular expressions and cannot figure out why this will not return punctuation marks.here is a piece of the text file the regex is parsing:APRIL/NNP is/VBZ …

cant find brokenaxes module

I want to create a histogram with broken axes and found that there must be a module doing this called brokenaxes that works together with matplotlib (source) . Anyway, when I trie to import the modul…

Python-Pandas While loop

I am having some trouble with the DataFrame and while loop:A B 5 10 5 10 10 5I am trying to have a while loop that:while (Column A < Column B):Column A = Column A + (Column B / 2)Colu…

split an enumerated text list into multiple columns

I have a dataframe column which is a an enumerated list of items and Im trying to split them into multiple columns. for example dataframe column that looks like this:ID ITEMSID_1 1. Fruit 12 oranges 2…

Python using self as an argument [closed]

This question is unlikely to help any future visitors; it is only relevant to a small geographic area, a specific moment in time, or an extraordinarily narrow situation that is not generally applicable…

how to shuffle questions in python

please anyone help me i have posted my whole work down can anyone tell me how to shuffle theses five questions it please i will be very thankful.print("welcome to the quiz") Validation = Fals…

Selenium python do test every 10 seconds

I am using selenium (python) testing and I need to test my application automatically every 10 seconds.How can I do this?

Type Object has no attribute

I am working on a program, but I am getting the error "Type object Card has no attribute fileName. Ive looked for answers to this, but none that Ive seen is in a similar case to this.class Card: R…

Generate random non repeating samples from an array of numbers

I made a battleships game and I now need to make sure that the computer doesnt attack at the same spot twice.My idea of it is storing each shots co-ordinates in a variable which gets added to whenever …

How to get back fo first element in list after reaching the end? [closed]

Closed. This question needs details or clarity. It is not currently accepting answers.Want to improve this question? Add details and clarify the problem by editing this post.Closed 5 years ago.Improve…