The following code snippet is from python cook book, 3rd Ed. Chapter 8.21:
class NodeVisitor:
def visit(self, node):
methname = 'visit_' + type(node).__name__
meth = getattr(self, methname, None)
if meth is None:
meth = self.generic_visit # this is the line that I have problem with
return meth(node)
def generic_visit(self, node):
raise RuntimeError('No {} method'.format('visit_' + type(node).__name__))
As I comment in the code, I have two questions about this line:
meth = self.generic_visit # this is the line that I have problem with
- Why self.generic_visit is parameterless?
- more important, generic_visit does nothing but raising a RuntimeError, how come it returned something and assigned to "meth"?
与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…