0

Rather than unpacking a list of fields, I was attempting to unpack a tuple of fields, and pass the result into the db().select() function, as in the following:

def get_record(self, record_id, fields):
    try:
        return self.db(self.db.t_group.id == record_id).select(*fields).first()
    except Exception as e:
        print e

def fun1(self, record_id):
    self.get_record(record_id, (self.db.t_group.f_expiration))

Doing so promptly gobbles up all of the system's memory, more or less bringing the system to a halt. If I had passed a list, instead of a tuple, however, as in the following statement, the function works as expected:

def fun1(self, record_id):
    self.get_record(record_id, [self.db.t_group.f_expiration])

Why is it problematic to unpack a tuple into the db().select() function?

Boa
  • 2,609
  • 1
  • 23
  • 38

1 Answers1

1

(self.db.t_group.f_expiration) is not a tuple but is simply equivalent to self.db.t_group.f_expiration.

In Python, tuples are defined by separating items with commas, not by surrounding items with parentheses (the parentheses are not always needed and simply make it possible to define the tuple expression within a larger context, as with any other expression). To create a one-item tuple, just add a comma after the item (and if necessary, wrap the whole thing in parentheses). In this case:

(self.db.t_group.f_expiration, )

See https://wiki.python.org/moin/TupleSyntax.

Anthony
  • 25,466
  • 3
  • 28
  • 57