-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathex42.py
38 lines (25 loc) · 826 Bytes
/
ex42.py
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
# Class Inheritance
class Book:
booksAreCool = True
def __init__(self, pages, author):
self.pages = pages
self.author = author
print("Calling Book constructor")
def bookMethod(self):
print("Calling book method")
class Ebook(Book):
def __init__(self, pages, author):
print("Calling Ebook constructor")
super(Ebook, self).__init__(pages, author)
def ebookMethod(self):
print("Calling ebook method")
e = Ebook(pages=10, author='Ben')
e.bookMethod()
e.ebookMethod()
e.__setattr__('booksAreCool', False)
print(e.booksAreCool)
# checking for classes and subclasses
if issubclass(Ebook, Book):
print(f"{Ebook.__name__} is a subclass of {Book.__name__}")
if isinstance(e, Book):
print(f"{e.__class__} is an instance of {Book.__name__}")