Function that knows the names of its actual parameters

Stefan Franke spamfranke at bigfoot.de
Mon Jun 19 12:36:37 EDT 2000


On 18 Jun 2000 21:53:57 GMT, aahz at netcom.com (Aahz Maruch) wrote:

>Why not simply write a function that takes a dict as its parameter?
That's what I do now.

>Maybe if you gave a better description of an occasion you think you need
>this feature we could give you a better answer.
Sure - see snippet below. Being able to call SQL_update without doubling
the arguments would be nice. 
If we could pass more than one of these dicts to SQL_update(), it would even 
be possible to specify the WHERE clause with current parameters as well.
I'm enclosing the magic arguments here into $...$ for provocative reasons <wink>:

>>> index=1
>>> member_name='you'
>>> is_current=1
>>> key=23
>>> SQL_update(table, $member_index, member_name$, $key, is_current$)
UPDATE my_table SET index=1, member_name='you' WHERE key=23 AND is_current=1

Of course no one really *needs* this feature, it's just syntactic sugar that doesn't
add to the expressive power of the language. In fact, writing this in a function
call notation possibly obfuscates things. 
So I'm not proposing anything here. I just feel a certain preference for musing
about those parts of Python were the border between source code identifiers 
and their string value is crossed (**kwargs, __getattr__, import, to name a few).

Hesitating-to-follow-the-longstanding-tradidtion-to-demand-a-Python-syntax-change-for-any-singular-special-case-you-stumble-upon-ly
y'rs,
Stefan


---------------------------------------
import string

def SQL_update(table, **set):
  return "UPDATE %s SET %s WHERE [...]" %(table, SQL_value_dict(set))

def SQL_value_dict(dict):
  args = []
  for k,v in dict.items():
    if v is not None:
      args.append("%s=%s" %(k, repr(v)))  # simplified: should be SQL_repr here
  return string.join(args, ", ")
 
member_index=1
member_name_before="ok"
is_current=None
print SQL_update("my_table", member_index=member_index,
                 member_name_before=member_name_before, is_current=is_current)




More information about the Python-list mailing list