title: Visitor pattern in python
body: <p>here is a simplified implementation of the visitor pattern in C++. Ist it possible to implement something like this in Python?</p>
  
  <p>I need it, because I will pass Objects from C++ code to a function in Python. My idea was to implement a visitor in Python to find out the type of the Object.</p>
  
  <p>My C++ code:</p>
  
  <pre><code>#include &lt;iostream&gt;
  #include &lt;string&gt;
  
  
  class t_element_base
  {
  public:
      virtual void accept( class t_visitor &amp;v ) = 0;
  };
  
  
  class t_element_deriv_one: public t_element_base
  {
  public:
      void accept( t_visitor &amp;v );
  
      std::string t_element_deriv_one_text()
      {
          return "t_element_deriv_one";
      }
  };
  
  
  class t_element_deriv_two: public t_element_base
  {
  public:
      void accept( t_visitor &amp;v );
  
      std::string t_element_deriv_two_text()
      {
          return "t_element_deriv_one";
      }
  };
  
  
  class t_visitor
  {
  public:
      void visit( t_element_deriv_one&amp; e ){ std::cout &lt;&lt; e.t_element_deriv_one_text() &lt;&lt; std::endl; }
      void visit( t_element_deriv_two&amp; e ){ std::cout &lt;&lt; e.t_element_deriv_two_text() &lt;&lt; std::endl; }
  };
  
  
  void t_element_deriv_one::accept( t_visitor &amp;v )
  {
      v.visit( *this );
  }
  
  void t_element_deriv_two::accept( t_visitor &amp;v )
  {
      v.visit( *this );
  }
  
  
  int
  main
  (
  void
  )
  {
      t_element_base* list[] =
      {
          new t_element_deriv_one(), new t_element_deriv_two()
      };
      t_visitor visitor;
  
      for( int i = 0; i &lt; 2; i++ )
          list[ i ]-&gt;accept( visitor );
  }
  </code></pre>