All quizzesHard
Advanced OOP — Series 2
Preview — 3 of 10 questions
What is the output of the following code?
javascript
from abc import ABC, abstractmethod
class Flyable(ABC):
@classmethod
def __subclasshook__(cls, subclass):
return hasattr(subclass, 'fly') and callable(subclass.fly)
class Bird:
def fly(self):
return "flap flap"
class Rock:
pass
print(issubclass(Bird, Flyable))
print(issubclass(Rock, Flyable))
print(isinstance(Bird(), Flyable))ATrue, False, True
BTrue, True, True — __subclasshook__ makes every class a virtual subclass of Flyable automatically.
CFalse, False, True
DTrue, False, False
What is the output of the following code?
javascript
class LoggedAttr:
def __init__(self):
self.value = None
def __get__(self, instance, owner):
return self.value
def __set__(self, instance, value):
self.value = value
def __delete__(self, instance):
print("deleting attribute")
self.value = None
class Widget:
color = LoggedAttr()
w = Widget()
w.color = "red"
print(w.color)
del w.color
print(w.color)Ared, then del w.color raises AttributeError: __delete__
Bred, then AttributeError: attribute 'color' cannot be deleted (no deleting attribute message)
Cred, deleting attribute, None
Dred, deleting attribute, red (the value is unaffected by deletion)
What is the output of the following code?
javascript
class Proxy:
def __init__(self):
self.real = 42
def __getattr__(self, name):
return f"fallback for {name}"
p = Proxy()
print(p.real)
print(p.missing)Afallback for real, fallback for missing
B42, AttributeError: 'Proxy' object has no attribute 'missing'
C42, None
D42, fallback for missing
Sign up free to play
Answer all 10 questions (7 more), see explanations for every answer, and track your score.