{"id":6297,"library":"visitor","title":"Visitor","description":"Visitor is a tiny Python library designed to facilitate the implementation of the visitor design pattern. It provides a base `Visitor` class to help separate algorithms from the object structures they operate on, addressing the peculiarities of dynamic typing in Python for this pattern. The library is very small, and its documentation suggests that users might consider copying the source code directly into their projects. The current version is 0.1.3, with the last release dating back to 2016, indicating a very mature or inactive maintenance cadence.","status":"maintenance","version":"0.1.3","language":"en","source_language":"en","source_url":"http://github.com/mbr/visitor","tags":["visitor pattern","design pattern","metaprogramming"],"install":[{"cmd":"pip install visitor","lang":"bash","label":"Install with pip"}],"dependencies":[],"imports":[{"symbol":"Visitor","correct":"from visitor import Visitor"}],"quickstart":{"code":"from visitor import Visitor\n\nclass JSONEncoder(Visitor):\n    def __init__(self):\n        self.output = []\n\n    def visit_list(self, obj):\n        self.output.append('[')\n        for item in obj:\n            self.visit(item)\n            self.output.append(',')\n        if len(obj) > 0: # Remove trailing comma if list is not empty\n            self.output.pop()\n        self.output.append(']')\n\n    def visit_dict(self, obj):\n        self.output.append('{')\n        for key, value in obj.items():\n            self.visit(key)\n            self.output.append(':')\n            self.visit(value)\n            self.output.append(',')\n        if len(obj) > 0: # Remove trailing comma if dict is not empty\n            self.output.pop()\n        self.output.append('}')\n\n    def visit_str(self, obj):\n        self.output.append(f'\"{obj}\"')\n\n    def visit_int(self, obj):\n        self.output.append(str(obj))\n\n    def visit_float(self, obj):\n        self.output.append(str(obj))\n\n    def generic_visit(self, obj):\n        # Fallback for types not explicitly handled, e.g., None, bool\n        self.output.append(str(obj).lower() if isinstance(obj, bool) else 'null' if obj is None else repr(obj))\n\n    def encode(self, obj):\n        self.output = []\n        self.visit(obj)\n        return ''.join(self.output)\n\n\n# Example Usage:\ndata = {\n    'name': 'Alice',\n    'age': 30,\n    'isStudent': False,\n    'courses': ['Math', 'Science'],\n    'address': {'city': 'New York', 'zip': 10001},\n    'grades': [95, 88.5, 76],\n    'null_field': None\n}\n\nencoder = JSONEncoder()\njson_string = encoder.encode(data)\nprint(json_string)\n\nexpected_output = '{\"name\":\"Alice\",\"age\":30,\"isStudent\":false,\"courses\":[\"Math\",\"Science\"],\"address\":{\"city\":\"New York\",\"zip\":10001},\"grades\":[95,88.5,76],\"null_field\":null}'\nassert json_string == expected_output\nprint(\"Output matches expected JSON.\")","lang":"python","description":"This quickstart demonstrates how to use the `Visitor` class to implement a simple JSON encoder. It defines a `JSONEncoder` class that inherits from `Visitor` and provides `visit_` methods for different Python types (list, dict, str, int, float). The `generic_visit` method acts as a fallback for unhandled types."},"warnings":[{"fix":"Be aware that the library is unmaintained. For new projects, consider implementing the visitor pattern manually or using a more actively developed library. If using this library, thorough testing with your target Python version is recommended.","message":"The library has not been updated since May 2016, with version 0.1.3 being the latest. While the core visitor pattern implementation is stable, it means the library is not actively maintained and may not leverage newer Python features or address modern compatibility concerns.","severity":"gotcha","affected_versions":"All versions (0.1.x)"},{"fix":"Evaluate whether including this as a dependency is necessary or if a direct copy of the source code (which is very small) might be more appropriate for your project's long-term maintenance strategy.","message":"The documentation itself suggests that the library is so small you might be better off copying and pasting the source directly into your project. This further emphasizes its lack of active development and its minimal footprint.","severity":"gotcha","affected_versions":"All versions (0.1.x)"},{"fix":"Avoid naming your own Python files or modules 'visitor.py' to prevent accidental shadowing. Always import with explicit names or aliases if conflicts arise, e.g., `import visitor as visitor_lib`.","message":"Naming a local file or module `visitor.py` can lead to a naming conflict, shadowing this installed library or the more common design pattern concept itself. This is a general Python footgun when using simple, descriptive names for modules.","severity":"gotcha","affected_versions":"All versions (0.1.x)"}],"env_vars":null,"last_verified":"2026-04-14T00:00:00.000Z","next_check":"2026-07-13T00:00:00.000Z"}