Intent

modular programming with visitors in Language:Python

Motivation

This contribution is similar to Contribution:javaVisitor. The Visitor Pattern allows the separation of operations from an object model.

Illustration

There is a method for every class to work on it because python has no method overloading. The dispatch is done by the Visitor class which uses reflection. It searches for a method which has the name visit [class name] but if there is not any method, it calls a default method that performs a deeper traversal.

class CutVisitor(Visitor):

    def visit Company(self, c):
        for d in c.depts:
            self.visit(d)

    def visit Department(self, d):
        self.visit(d.manager)
        for s in d.subunits:
            self.visit(s)

    def visit Employee(self, e):
        e.salary /= 2.0

Usage

Download the source and run python operations.py


There are no revisions for this page.

User contributions

    This user never has never made submissions.

    User edits

    Syntax for editing wiki

    For you are available next options:

    will make text bold.

    will make text italic.

    will make text underlined.

    will make text striked.

    will allow you to paste code headline into the page.

    will allow you to link into the page.

    will allow you to paste code with syntax highlight into the page. You will need to define used programming language.

    will allow you to paste image into the page.

    is list with bullets.

    is list with numbers.

    will allow your to insert slideshare presentation into the page. You need to copy link to presentation and insert it as parameter in this tag.

    will allow your to insert youtube video into the page. You need to copy link to youtube page with video and insert it as parameter in this tag.

    will allow your to insert code snippets from @worker.

    Syntax for editing wiki

    For you are available next options:

    will make text bold.

    will make text italic.

    will make text underlined.

    will make text striked.

    will allow you to paste code headline into the page.

    will allow you to link into the page.

    will allow you to paste code with syntax highlight into the page. You will need to define used programming language.

    will allow you to paste image into the page.

    is list with bullets.

    is list with numbers.

    will allow your to insert slideshare presentation into the page. You need to copy link to presentation and insert it as parameter in this tag.

    will allow your to insert youtube video into the page. You need to copy link to youtube page with video and insert it as parameter in this tag.

    will allow your to insert code snippets from @worker.