title: psycopg2 execute_values with list of class objects
body: <p>I have a class <code>Person</code> with <code>id</code>, <code>fname</code>, and <code>lname</code>. My program will have a large list of <code>Person</code> objects, which I would like to insert into my <code>PostgreSQL</code> database.</p>
  
  <p>I would like to do this through <code>psycopg2.execute_values</code> method, which requires <code>__getitem__</code> to be defined for my class. </p>
  
  <p><code>__getitem__</code> is currently returning <code>tuple(id, fname, lname)</code>, which results in <code>pscopg2.execute_values</code> trying to execute: <code>insert into persons(id, fname, lname) values ((1, 'test1', 'test1'))...</code></p>
  
  <pre><code>import psycopg2.extras
  
  
  class Person:
      def __init__(self, id, fname, lname):
          self.id = id
          self.fname = fname
          self.lname = lname
  
      def __len__(self):
          return 1
  
      def __getitem__(self, item):
          return self.id, self.fname, self.lname
  
  
  dsn = "user=my_user host=localhost port=5432 dbname=my_db"
  query = "insert into persons(id, fname, lname) values %s"
  
  p1 = Person(1, 'test1', 'test1')
  p2 = Person(2, 'test2', 'test2')
  
  records = list()
  records.append(p1)
  records.append(p2)
  with psycopg2.connect(dsn=dsn) as conn:
      with conn.cursor() as cursor:
          psycopg2.extras.execute_values(cursor, query, records, template=None, page_size=1000)
  
  psycopg2.ProgrammingError: INSERT has more target columns than expressions
  LINE 1: insert into persons(id, fname, lname) values ((1, 'test1', 'test1'))...
                                  ^
  HINT:  The insertion source is a row expression containing the same number of columns expected by the INSERT. Did you accidentally use extra parentheses?
  </code></pre>
  
  <p>Using a <code>namedtuple</code> works:</p>
  
  <pre><code>from collections import namedtuple
  import psycopg2.extras
  
  
  Person = namedtuple('Person', ['id', 'fname', 'lname'])
  
  dsn = "user=my_user host=localhost port=5432 dbname=my_db"
  query = "insert into persons(id, fname, lname) values %s"
  
  p1 = Person(1, 'test1', 'test1')
  p2 = Person(2, 'test2', 'test2')
  
  records = list()
  records.append(p1)
  records.append(p2)
  
  with psycopg2.connect(dsn=dsn) as conn:
      with conn.cursor() as cursor:
          psycopg2.extras.execute_values(cursor, query, records, template=None, page_size=1000)
  </code></pre>
  
  <p>However, I want to implement a different <code>__eq__</code> and <code>__hash__</code> implementation. If there is a way to override these methods for a <code>namedtuple</code> then I'm willing to go with that approach.</p>
  
  <p>Otherwise how can I modify <code>__getitem__</code> to return <code>id</code>, <code>fname</code>, and <code>lname</code> separately so that the query becomes:</p>
  
  <pre><code>insert into persons(id, fname, lname) values (1, 'test1', 'test1')...
  </code></pre>