Python inheritance def bases(aclass): We learned that when we declare a class us
ID: 3843844 • Letter: P
Question
Python inheritance
def bases(aclass):
We learned that when we declare a class using inheritance, the bases attribute of the class is bound to a tuple of references to its base classes. Write a recursive function named bases that takes a reference to any class and returns a set containing that class and all its base classes (back to object). You may not use the ________ mro or _______ mro attributes of the class, which would trivialize your function. You may use both iteration (over the ______ bases _______ list) and recursion. For example, given the following class definitions in a script class F:pass class C:pass class G:pass class B (F): pass class D (6): pass class A (B, C, D):pass Calling bases (A) returns the set {, , , , , , }Explanation / Answer
Solution:
class F : pass
class C : pass
class G : pass
class B(F) : pass
class D(G) : pass
class A(B,C,D) : pass
val=set
# recursive function
def bases(aclass):
return ([aclass]+ map(bases,aclass.__bases__))
Result:
>>> bases(A)
[<class __main__.A at 0x0275E960>, [<class __main__.B at 0x0275E900>, [<class __main__.F at 0x0275E870>]], [<class __main__.C at 0x0275E8A0>], [<class __main__.D at 0x0275E930>, [<class __main__.G at 0x0275E8D0>]]]
Related Questions
Navigate
Integrity-first tutoring: explanations and feedback only — we do not complete graded work. Learn more.