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?

5
  • 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). Commented Jun 27, 2021 at 1: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. Commented Jun 27, 2021 at 1:24
  • Probably what you are looking is: stackoverflow.com/a/29256784/8121377 Or the accepted answer in that same thread is also another option. Commented Jun 27, 2021 at 1:40
  • Per Felix K Jose, does this answer your question? Converting an object into a subclass in Python? Commented Jun 27, 2021 at 4: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. Commented Jul 8, 2021 at 0:12

1 Answer 1

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

Sign up to request clarification or add additional context in comments.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.