PYTHON: relational data between classes in OOP and how to use them the right way

Viewed 31

i´m new to OOP and trying to get the right programming style. Currently i´m trying to work with three classes. One Parent- and Child-Class and a third class. The data between the classes should be connected. I know concepts of 'relational databases', but i´m not shure if i need some kind of secondary key in one class, to connect the other one in a flexible way.

Should i work with dictionaries instead, when i don´t want to integrate an external database?

Please have a closer look @ my comments in the code.

#Inheritance Person -> Customer
class Person:
    def __init__(self, person_id, firstname):
        self.person_id = person_id
        self.firstname = firstname
        # ...

class Customer(Person):
    def __init__(self, person_id, firstname, customer_level):
        super().__init__(person_id, firstname)               
        self.customer_level = customer_level
        # ...

    def get_customer_level(self):
        return self.customer_level

class BillFormular():
    def __init__(self, bill_id, customer_id, is_paid=False):
        self.bill_id = bill_id
        # currently i store the customer_id (as some kind of secondary key for 
        # the person_id in the inhereting BillFormular(Person)-Class, as i don´t  
        # have a relational database. Is that a good way?
        self.customer_id = customer_id          
        self.is_paid = is_paid
        # ...

    def print_bill(self):
        # the Argument 'judith' works, if i want to connect the other classes methods
        # But how to make it more flexible, e. g. by using the stored 'customer_id' 
        # in the BillFormular(Person)-Class and the 'person_id' in the Person-Class
    
        print(f'Your Status from external class: {Customer.get_customer_level(judith)}')

judith = Customer(22, 'Judith', 3)
bill42 = BillFormular(42, 22)
bill42.print_bill()

Thank you!

0 Answers
Related