Skip to content

Instantly share code, notes, and snippets.

@rdcoder33
Created December 27, 2018 04:54
Show Gist options
  • Save rdcoder33/5bacf8e7f750fd8eae2ee9697dd0d410 to your computer and use it in GitHub Desktop.
Save rdcoder33/5bacf8e7f750fd8eae2ee9697dd0d410 to your computer and use it in GitHub Desktop.
class Book:
def __init__(self, name, author, publisher):
self.name = name
self.author = author
self.publisher = publisher
def book_details(self):
return f'{self.name} by {self.author} from {self.publisher}'
class Chapter:
def __init__(self, title, chap_num, book_obj):
self.title = title
self.chap_num = chap_num
self.book_obj = book_obj
def chapter_details(self):
return f'Chapter {self.chap_num}: {self.title}, is taken from {self.book_obj.book_details()}'
# Creating Book object
book_obj = Book('Best Book', 'RD', 'RD-pubs')
# Passing Book object to Chapter Book
first_chap = Chapter('Composition', 1, book_obj)
print(first_chap.chapter_details())
# accessing class Book properties and methods using Chapter Class object
print(first_chap.book_obj.author)
print(first_chap.book_obj.book_details())
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment