From the course: Python Object-Oriented Programming

Unlock the full course today

Join today to access over 24,500 courses taught by industry experts.

Solution: Data classes

Solution: Data classes

(upbeat music) - [Instructor] All right, for this challenge we had to convert an existing class structure to use Python data classes. So of course we need to import that module, which I did here. And then I redefined each class using the data class decorator. The base asset class contains the price property. Since that property is common to both bonds and stocks and it's declared as being a float. And then I declared the stock and bond data classes with their properties. So the stock class gets the ticker and description, which are both strings, and the bond class gets the description, duration, and interest rate properties. The asset class also had to override the comparison magic methods to support comparison operations and I put this logic in the asset class because the price property is common to both stocks and bonds and this enables us to compare stock and bond objects to each other directly. And one of the other things we needed to do to make this work correctly is remember to…

Contents