2

Say I have a base class called Publication:

class Publication():
    def __init__(self, title='Default', price='Default')
        self.title = title
        self.price = price

    def gettitle(self):
        self.title = input("Please type in a title: ")

    def getprice(self):
        self.price = input("Please type in a price: ")

And a subclass called Book that inherits from Publication:

class Book(Publication):
    def __init__(self, title, author, pages, current_page, price):
        super().__init__(title, price)
        self.author = author
        self.pages = pages
        self.current_page = current_page
    def turnpage(self.current_page):
        self.current_page += 1

Let's say early on in the course of my program, I create an object from the Publication class:

item = Publication()

But later on I have to assign my title and my price:

item.gettitle()

item.getprice()

I input "Lord of the Rings" and 10.99 respectively so now

item.title = "Lord of the Rings" 

and

item.price = 10.99

Now, let's say I have some logic in my program that, depending on the price or if the title matches a string in a list, then we know specifically it is a book (as opposed to something like a magazine or a newspaper) so now I'd like it to be

item = Book()

When I originally created my object from the Publication class, I didn't know it was a Book. But now that I know, is there a way for me to keep the attributes/methods bound to the originally created object while "extending"/"inheriting" the object (not sure if those are the right words) with the newly available attributes from the Book class?

ai88
  • 33
  • 4
  • 2
    If you mean reusing original object of superclass to create an object of subclass, you can write static factory method for it. For example, `Book.from_publication(item)`. – Boseong Choi Jun 27 '21 at 01:17
  • I've never heard of a static factory method but it might be along those lines. My main issue is that I don't want to lose the values of the attributes already obtained in the program for the earlier object while also enjoying the methods and expanded selection of attributes particular to the newer class. – ai88 Jun 27 '21 at 01:24
  • Probably what you are looking is: https://stackoverflow.com/a/29256784/8121377 Or the accepted answer in that same thread is also another option. – Felix K Jose Jun 27 '21 at 01:40
  • Per Felix K Jose, does this answer your question? [Converting an object into a subclass in Python?](https://stackoverflow.com/questions/597199/converting-an-object-into-a-subclass-in-python) – Davis Herring Jun 27 '21 at 04:25
  • I think I have to improve my knowledge to understand the answer so I'm not really sure. I'm pretty new to the whole concept of OOP and it took me quite a bit just to be able to formulate it. – ai88 Jul 08 '21 at 00:12

1 Answers1

0
item = Book(title=item.gettitle(), price=item.getprice())

Or you can add a method in Publication that will give you the array which contains all characteristics of class, and in the __init__ function get all needed parameters by array. Another way is to pass item as an argument so __init__ will get all attributes from created object

xezo360hye
  • 15
  • 6