question on global variables

Mark McEahern marklists at mceahern.com
Thu Oct 10 14:41:00 CEST 2002


[andy surany]
> There is data that I am returning - but this particular item happens
> to be the number of records returned from a query (the data set is
> returned). And I am using it everywhere. It is set in the query
> function - which is called once. I tried using different calling and
> return options, and finally came to the conclusion that what I
> really did want was the gloabal variable.

In other words, you're caching the results?

class Results(object):

  def __init__(self, results):
    self.results = results

  def getCount(self):
    return len(self.results)

  count = property(getCount)

sql = "select * from whatever;"
conn = db.open(connStr)
raw_results = conn.execute(sql)
results = Results(results)

***

Now you can pass results around to your heart's content and get the count of
the results without ever having to execute the SQL again.  I still don't
understand why you need a global variable.  You realize most folks eschew
their use?  Why?  Because they riddle your code with dependencies that are
hard to analyze, test, debug, etc.

Cheers,

// mark

-





More information about the Python-list mailing list