Python Forum
confusion on semantics of code
Thread Rating:
  • 0 Vote(s) - 0 Average
  • 1
  • 2
  • 3
  • 4
  • 5
confusion on semantics of code
#1
Hello,

I am working through a tutorial and lack the foundations to understand the following code
class NodeVisitor(object):
    def visit(self, node):
        method_name = 'visit_' + type(node).__name__
        visitor = getattr(self, method_name, self.generic_visit)
        return visitor(node)
Can someone tell me what the visit method does? Many thanks!
Reply
#2
I guess you found this by working with the ast module. What the function does is this (pseudopseudocode)
def visit(self, node):
    get the name of the type of the node, for example 'foo'
    if self has a method named 'visit_foo':
        return self.visit_foo(node)
    else:
        return self.generic_visit(node)
Reply
#3
Yes, I was working with the AST module. Thanks :)
Reply


Forum Jump:

User Panel Messages

Announcements
Announcement #1 8/1/2020
Announcement #2 8/2/2020
Announcement #3 8/6/2020