More Pythonic default args
Skip Montanaro <[email protected]> Mon, 7 Jun 2004 09:01:49 -0500
| Newsgroups | gmane.comp.python.sybase |
|---|---|
| Message-ID | <[email protected]> |
It seems more Pythonic to use None as a default parameter value instead of mutable objects like dictionaries and lists, even when the parameters aren't modified by the function. A simple patch for execute() and executemany() is attached. -- Skip Montanaro Got gigs? http://www.musi-cal.com/submit.html Got spam? http://www.spambayes.org/ [email protected]
Sybase.diff
(application/octet-stream, 1.6 KB)
*** Sybase.py~ Fri May 28 14:01:29 2004
--- Sybase.py Mon Jun 7 08:56:18 2004
***************
*** 700,706 ****
self._fetcher = None
self._closed = 1
! def execute(self, sql, params = {}):
'''DB-API Cursor.execute()
'''
_ctx.debug_msg('Cursor.execute\n')
--- 700,706 ----
self._fetcher = None
self._closed = 1
! def execute(self, sql, params = None):
'''DB-API Cursor.execute()
'''
_ctx.debug_msg('Cursor.execute\n')
***************
*** 708,713 ****
--- 708,715 ----
raise ProgrammingError('cursor is closed')
self._lock()
try:
+ if params is None:
+ params = {}
# Discard any previous results
self._fetcher = None
***************
*** 725,731 ****
finally:
self._unlock()
! def executemany(self, sql, params_seq = []):
'''DB-API Cursor.executemany()
'''
_ctx.debug_msg('Cursor.executemany\n')
--- 727,733 ----
finally:
self._unlock()
! def executemany(self, sql, params_seq = None):
'''DB-API Cursor.executemany()
'''
_ctx.debug_msg('Cursor.executemany\n')
***************
*** 733,738 ****
--- 735,742 ----
raise ProgrammingError('cursor is closed')
self._lock()
try:
+ if params_seq is None:
+ params_seq = []
for params in params_seq:
self.execute(sql, params)
if not self._fetcher._is_idle():