Velocity Reviews - Computer Hardware Reviews

Velocity Reviews > Newsgroups > Programming > Python > functions and named keyword arguments

Reply
Thread Tools

functions and named keyword arguments

 
 
Fuzzyman
Guest
Posts: n/a
 
      02-21-2005
Sorry if this is a duplicate - I use the google interface and sometiems
it screws up (not showing stuff you've posted *or* not posting it).
Before you ask it's because at work I have no NNTP and *heavily*
restricted http.

A colleague and I have built a Validator object for use with ConfigObj
and other general schema situations. A config file is used to store a
schema that specifies how to test a value that it is valid.

keyword=function(param1, param2)

e.g. you could specify :
size = range(30, 50)

This means that the keyword 'size' must be an integer between 30 and
50. There is a matching function stored in a dictionary that is called
and the relavant arguments passed to it.

The function name and parameters are parsed from the config file that
defines the schema.

args, fname = parsefunction('range(30,50)')
test = funcdict[fname](*args)

What I can't easily see is any way of passing named keyword arguments
to the function. Suppose we wanted to pass keyword=param to a function
- is there any way of doing this ... obviously passing in
'keyword=param' as text has entirely the wrong result......

Anyone got any clues ? I suspect compiling the function into a code
object will help.... but this is a slightly black art for me.

Regards,

Fuzzy
http://www.voidspace.org.uk/python/index.shtml

 
Reply With Quote
 
 
 
 
Diez B. Roggisch
Guest
Posts: n/a
 
      02-21-2005
> Sorry if this is a duplicate - I use the google interface and sometiems
> it screws up (not showing stuff you've posted *or* not posting it).
> Before you ask it's because at work I have no NNTP and *heavily*
> restricted http.


It is - so I requote my answer


Im not sure if I understand you fully, but if what you are after is how to
pass named parameters analog to positional args, do it as dict:

def foo(name=None):
****print*name

foo(**{name: "Fuzzy"})

--
Regards,

Diez B. Roggisch
 
Reply With Quote
 
 
 
 
Fuzzyman
Guest
Posts: n/a
 
      02-22-2005

Diez B. Roggisch wrote:
> > Sorry if this is a duplicate - I use the google interface and

sometiems
> > it screws up (not showing stuff you've posted *or* not posting it).
> > Before you ask it's because at work I have no NNTP and *heavily*
> > restricted http.

>
> It is - so I requote my answer
>
>
> Im not sure if I understand you fully, but if what you are after is

how to
> pass named parameters analog to positional args, do it as dict:
>
> def foo(name=None):
> print name
>
> foo(**{name: "Fuzzy"})
>


Brilliant - that's exactly what I was looking for.... thanks.

Regards,

Fuzzy
http://www.voidspace.org.uk/python/index.shtml

> --
> Regards,
>
> Diez B. Roggisch


 
Reply With Quote
 
 
 
Reply

Thread Tools

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is On
HTML code is Off
Trackbacks are On
Pingbacks are On
Refbacks are Off


Similar Threads
Thread Thread Starter Forum Replies Last Post
[ANN] rdp-arguments -- named arguments for 1.8 and 1.9! Roger Pack Ruby 2 01-04-2010 02:56 AM
(named|keyword) arguments gem Macario Ortega Ruby 0 08-07-2009 04:38 AM
Keyword arguments in a block, possible with zero arguments? Peter Motzfeldt Ruby 1 03-13-2007 01:33 PM
Difference between default arguments and keyword arguments Edward Diener Python 14 04-05-2004 11:26 PM
How to call functions with list and keyword arguments? John Z. Smith Python 4 11-29-2003 05:06 PM



Advertisments