[GRASSGUI] how to deal with *args,**kwds

Daniel Calvelo dca.gis at gmail.com
Tue Apr 10 03:28:17 EDT 2007


Michael,

The *a and **b syntax is used to slurp up respectively any non-keyword
argument and any keyworded argument into respectively an array a and a
dictionary b. If memory serves right, it was added to the language
around version 1.6 to do just this kind of things: passing "all
remaining arguments" to another function.

For example:

def inner( *bare_args, **key_args ):
  for arg in bare_args:
    print arg
  for (key,arg) in key_args.items():
    print key,":",arg

def outer( one_compulsory_arg, a_compulsory_keyworded_arg = None, *a, **b ):
  inner( one_compulsory_arg, a_compulsory_keyworded_arg, *a, **b)
  print
  inner( 1,2, rest=3, more="less")
  print

outer( "one","two","three","four", five="5", six="6" )

So, in order to "pass-through" the *arguments and the
**keyword_arguments, just pass them as arguments, keeping the
asterisks.

For your problem, you may either:

a) put a type keyword argument explicitly, or
b) catch the type argument from within the **kwargs dictionary, like this:

if kwargs.has_key('type'):
  # do the thing

or

has_the_type = kwargs.get('type',None) #This results in None if there
is no 'type' key

I'd go for a), since the interface becomes explicit and documented,
but maybe b) suits you better.

Cheers,

Daniel.

On 4/9/07, Michael Barton <michael.barton at asu.edu> wrote:
>
>  While I understand the underlying concept, I've never been able to get the
> *args, **kwargs structure to work for me.
>
>  I'm trying to change the custom select control I made and working with a
> popup control as an alternative to the combo control I used before.
>
>  It begins with the following syntax and I'd like to pass a couple other
> arguments (at least "type") to the class. Here is the syntax.
>
>      def __init__(self,*_args,**_kwargs):
> apply(pop.PopupControl.__init__,(self,) + _args,_kwargs) How can I pass
> 'type' here? Where do I put it?
>
>  Note that I'm unconvinced that this will fix the weird problem of the
> expanded tree node in the current select control preventing some systems
> from adding additional layers in the GIS Manager.
>
>  Thanks
>  Michael
>  __________________________________________
>  Michael Barton, Professor of Anthropology
>  School of Human Evolution & Social Change
>  Center for Social Dynamics & Complexity
>  Arizona State University
>
>  phone: 480-965-6213
>  fax: 480-965-7671
>  www: http://www.public.asu.edu/~cmbarton
>
>
> _______________________________________________
> grassgui mailing list
> grassgui at grass.itc.it
> http://grass.itc.it/mailman/listinfo/grassgui
>
>


-- 
-- Daniel Calvelo Aros




More information about the grass-gui mailing list