multiple inheritance and method resolution order

I have a standard exercise in sharing Python's data structures, wherein I bury the string "Waldo" in some deeply nested object, say as an element in a tuple in a dict in a list... to some demented level.[1] When we're learning Python grammar, that's when "demented" makes sense i.e. this isn't about writing production code so much as getting the basic principles. Along similar lines, when introducing super() and the method resolution order, I'm often looking for a class hierarchy that (A) involves multiple inheritance, (B) is several levels deep and (C) has "diamond patterns". Finally it hit me: I could turn to the Book of Genesis for a family tree with all these features, plus the advantage of being easy to lookup. Adam and Eve had several kids (they lived enormously long lives by today's standards), and the siblings had to have kids by each other given they were the only humans on the planet. Adam and Eve's son Seth and daughter Azura (two cases of multiple inheritance), had a son (another case). Also, Noah's mom goes back to a great grandparent shared with his dad. Diamond pattern. Eureka. There's already some precedent in mixing CS with bible studies (thinking of Knuth), and besides, Python is heavily used in the humanities for natural language processing. Bridging from Python to Genesis doesn't seem too far-fetched. :-D http://nbviewer.jupyter.org/github/4dsolutions/SAISOFT/blob/master/OO_Paradi... (see "Multiple Inheritance") There "where's Waldo" exercise and this investigation into the MRO may be combined: bury a waldo() instance or class method somewhere in the Genesis family try, in a couple places and go:
subject = Noah() subject.waldo()
to find out where the name resolves. Have the waldo method report on its class. Kirby Cross reference to connected edu-sig topic: Rich Data Structures [1] Example code: # -*- coding: utf-8 -*- """ Created on Mon Apr 17 17:23:45 2017 @author: Kirby Urner Example of the "Where's Waldo" genre: https://i.ytimg.com/vi/SiYrSYd7mlc/maxresdefault.jpg Extract "Waldo" from each data structure """ data = {"id:":["Joe", "Smith"], "mother": ["Judy", "Smith"], "father": ["Waldo", "Smith"]} waldo = "???" # output "Waldo" print(waldo) #============================ data = {"Waldo": {"scores":[34,56,23,98,89]}} waldo = "???" # output "Waldo" hint: dict.keys() print(waldo) #============================ data = {(1,2):{"Name":["Waldeen", "Smith"]}, (4,5):{"Name":["Waldorf", "Smith"]}, (9,0):{"Name":["Waldo", "Smith"]}} waldo = "???" # output "Waldo" hint: tuples may be keys print(waldo) #============================ data = ["Joe", 3, 7, ["dog", ("cat", "Waldo")], 27, {}] waldo = "???" print(waldo) # output "Waldo' #============================ data = ([], [], ()) data[1].append("Wendy") data[1].append("Waldo") data[1].append("Willow") # where's Waldo? waldo = "???" # <your answer> print(waldo) # NOW MAKE UP SOME OF YOUR OWN!

Addendum (copyleft, re-use and modify at will) + related links [The]
"where's Waldo" exercise and this investigation into the MRO may be combined: bury a waldo() instance or class method somewhere in the Genesis family [tree],
=== class Gen0 (object): """the Old One""" def __init__(self): print("__init__ of {}".format("Gen0")) class Adam(Gen0): """one of two in Gen1""" def __init__(self): super().__init__() print("__init__ of {}".format("Adam")) class Eve(Gen0): """one of two in Gen1""" def __init__(self): super().__init__() print("__init__ of {}".format("Eve")) def waldo(self): print("Waldo in {}".format("Eve")) class Cain(Adam, Eve): """Gen2""" def __init__(self): super().__init__() print("__init__ of {}".format("Cain")) class Abel(Adam, Eve): """Gen2""" def __init__(self): super().__init__() print("__init__ of {}".format("Abel")) class Seth(Adam, Eve): """Gen2""" def __init__(self): super().__init__() print("__init__ of {}".format("Seth")) class Azura(Adam, Eve): """Gen2""" def __init__(self): super().__init__() print("__init__ of {}".format("Azura")) class Enosh(Seth, Azura): """Gen3""" def __init__(self): super().__init__() print("__init__ of {}".format("Enosh")) class Kenan(Enosh): """Gen4""" def __init__(self): super().__init__() print("__init__ of {}".format("Kenan")) class Mahalaleel(Kenan): """Gen5""" def __init__(self): super().__init__() print("__init__ of {}".format("Mahalaleel")) class Jared(Mahalaleel): """Gen6""" def __init__(self): super().__init__() print("__init__ of {}".format("Jared")) class Enoch(Jared): """Gen7""" def __init__(self): super().__init__() print("__init__ of {}".format("Enoch")) class Methusela(Enoch): """Gen8""" def __init__(self): super().__init__() print("__init__ of {}".format("Methusela")) def waldo(self): print("Waldo in {}".format("Methusela")) class Elisha(Enoch): """Gen8""" def __init__(self): super().__init__() print("__init__ of {}".format("Elisha")) class Lamech(Methusela): """Gen9""" def __init__(self): super().__init__() print("__init__ of {}".format("Lamech")) class Ashmua(Elisha): """Gen9""" def __init__(self): super().__init__() print("__init__ of {}".format("Ashmua")) def waldo(self): print("Waldo in {}".format("Ashuma")) class Noah(Lamech, Ashmua): """Gen10""" def __init__(self): super().__init__() print("__init__ of {}".format("Noah")) if __name__ == "__main__": import inspect subject = Noah() subject.waldo() print(inspect.getmro(subject.__class__)) === Related posts: https://grokbase.com/t/python/edu-sig/066kd86zh1/rich-data-structures-plus-k... https://mail.python.org/pipermail/edu-sig/2012-December/010709.html
participants (1)
-
kirby urner