0

Not sure how to form corectly, but the problem is related to a shapefile(python library) function of writing a new row, which requires to write variables.. From my data I'm creating a list, which I thought I can cast to tuple and will solve my problem, but nah.. I doesn't go so easy...

The function is asking this: w.record(a, b, c, d....) or w.record('a', 256, 25.2444, 'd'....) My data is in a list, but the list length is not known my default, it depends form the input data and the right length is important!

I could also write w.record(list[0], list[1]...list[len(list)-1]), but then I have to create this automatically and not sure how could I do that.

Btw, I use an old python: 2.5... (not my choise)

najuste
  • 234
  • 3
  • 15
  • 1
    I'm not sure I understand your question correctly; but I Python supports functions with [arbitrary argument lists](http://docs.python.org/tutorial/controlflow.html#arbitrary-argument-lists), is that what you need? – Burhan Khalid Oct 18 '12 at 09:25

2 Answers2

1

There are 4 kinds of argument in a function declaration: -

  • Positional Argument
  • Default Argument (or, keyword argument - in the form: - a = 5)
  • Non-Keyword Argument
  • Keyword Argument (or, dictionary type keyword argument)

A typical function declaration goes like this: -

func(positional_args, keyword_args,
                     *tuple_grp_nonkw_args, **dict_grp_kw_args)

You need non-keyword argument, which take extra argument and store them as tuple:

w.record(*yourvalues)

Similarly keyword argument, takes dictionary as input. They are denoted with **kwarg.

  • All the keyword arguments after the non-keyword arguments go into **kwarg
  • And all the keyword arguments before the non-keyword arguments are default arguments

For E.G: -

// nkwarg takes a list
// kwarg takes a dictionary
def foo(arg, defaultarg = 5, *nkwarg, **kwarg):
    print arg
    print defaultarg
    print nkwarg
    print kwarg


foo("hello", ["how", "are", "you"], a = 3, b = 4)

OUTPUT: -

hello
5
(['how', 'are', 'you'],)
{'a': 3, 'b': 4}
Rohit Jain
  • 209,639
  • 45
  • 409
  • 525
  • but if I have a list: l=['Mok', 'Union_80vH_50vH', 13867, 32, '11:20:13.077', '523.343', '29.1', '29.09.2012', '13:17:02.077', '11:19:58.077', 1, 35, '90', '523.343', 1348917598], how does it go? – najuste Oct 18 '12 at 09:30
  • @najuste It will be stored in `*nkwarg`. `*` mark is used to make a parameter take any number of values. Basically it takes a variable length list. – Rohit Jain Oct 18 '12 at 09:32
  • @najuste You can take a look at this link: - http://stackoverflow.com/questions/1419046/python-normal-arguments-vs-keyword-arguments and http://docs.python.org/release/1.5.1p1/tut/keywordArgs.html – Rohit Jain Oct 18 '12 at 09:34
  • Thanks, did not know about * things (newbie). But if I pass a list, as a result from such I again get a list.. Ok, kind of find I can use w.record(foo(*list)) but that's agian happens with two brackets and so does not work.. I got it!! Sorry, I just tested wrongly.. uf.. – najuste Oct 18 '12 at 09:49
0
def foo(hello, *args):
 print hello

 for each in args:
     print each

if __name__ == '__main__':
 foo("LOVE", ["lol", "lololol"])
Vivek S
  • 5,384
  • 8
  • 51
  • 72