-2

Let's say I have a Patron class that has the instance variables: name, patron_id and borroweds(borrowed books). This pretty much is a class for a virtual library. if one of my functions requires me to take a book_id # which is a string and to "reshelve" this book. How would I add the string to my class? This is what I have:

class Patron:
    name= ""
    patron_id= ""
    borroweds= list()
    # the class constructor
    def __init__(self, name, patron_id, borroweds):
        self.name= name
        self.patron_id= patron_id
        self.borroweds= borroweds
    def __str__(self):
        s= str("Patron("+str(self.name)+","+str(self.patron_id)+","
            +list(self.borroweds)")"
        return s
    def __repr__(self):
        return str(self)
    def return_book(self,library,book, book_id):
        print("Can you please reshelve this book?" + book_id)

The last function is what I need some help with.

Martijn Pieters
  • 1,048,767
  • 296
  • 4,058
  • 3,343
user2976821
  • 353
  • 1
  • 3
  • 8

1 Answers1

0

Well if that library is an instance of some Library class and it provides methods for "reshelving" a book then you should be able to just say

def return_book(self,library,book, book_id):
    print("Can you please reshelve this book?" + book_id)
    library.shelve_book(book)

If you're asking how you are supposed to write your Library class or how to write the shelve_book method, well, that really depends on how you're storing those books.

For example if you store it as a list of book objects or something

self.books = []

Then you could say

class Library(object):

    def self.shelve_book(self, book):
        self.books.append(book)
MxLDevs
  • 19,048
  • 36
  • 123
  • 194
  • What if it says to create a library object that is empty? does that mean it can still be a list? – user2976821 Dec 03 '13 at 19:26
  • That is up to you to figure out. I don't know what your requirements are. An empty list is just a list without any elements. – MxLDevs Dec 03 '13 at 19:39
  • But what does it mean to have an empty object? – user2976821 Dec 03 '13 at 19:39
  • "Empty" depends on what object you're talking about. An "Empty String" is a string without any characters. An empty Dictionary is a dictionary without any key-value pairs, etc. – MxLDevs Dec 03 '13 at 19:40
  • ok what I think I'm supposed to do is make a class called Library with 2 empty lists, one for books and one for patrons. Thanks! – user2976821 Dec 03 '13 at 19:42