[Tutor] associating two objects without ORM and processing a text file

Dave Angel davea at davea.name
Mon Feb 11 19:36:42 CET 2013

On 02/11/2013 01:19 PM, Alan Gauld wrote:
> On 11/02/13 05:14, neubyr wrote:
>> <snip>
>> * How do I associate/relate Book and Author classes so that it will help
>> me in getting information like 'get list of books written by an author'?
>> Data attribute?
> I woudn't have a separate Author class but, if you must, something like:
> class Book:
>    def __init__(self, theAuthor,theTitle):
>        self.Author = theAuthor
>        self.title = theTitle
> class Author:
>    def __init__(self,aName):
>       self.name = aName
> myBook = Book(Author('Jane Austin'), 'Pride & Prejudice')

Big problem with that;  then there may be multiple instances of Author, 
representing the same Author.  Instead, there needs to be a factory 
function which reuses the same Author objects if an additional book with 
the same author is encountered.  Without such an approach, one might as 
well stick with strings, which is what we each recommended.


More information about the Tutor mailing list