r/learnpython • • 11m ago

How to not clobber a class when inheriting

I wrote a visitor to extract some datas via the ast

class VariableFinder(ast.NodeTransformer):
    """ Visitor pattern impl."""
    def __init__(self, find_object:str, find_method:str):
        """ 
        filter for only method calls that match find_object.find_method 
        store the line number and store the value or args passed to the call
        """
        self.find_method = find_method
        self.find_object = find_object
        self.found = []
...

And it struck me this is not C++, so how do we stop ourselves accidentally blatting/clobbering over ast.NoteTransformer attributes? I'm missing something and my searches are all indicating that the visitor pattern is frowned upon. Is there an alternative way to do these safely?

1 Upvotes

0 comments sorted by