You are not logged in.
can somebody explain to me what the single and double astricks are doing? Printing them back to me made absolutely no sense. Gave me what apeared to be a half-ass tuple and an empty dict?
I had no luck searching google and really have no idea what they're even called.
Offline
Dive into python link -- Arbitrary Argument Lists
Off the cuff, I think it is that every argument passed with a name=value, gets stuffed into the **variable. Every single value gets stuffed into the *variable.
def function(a, b, *list, **dict):
fun_call(4,5,rsb="rock",(2,3),1)
Then..
a = 4
a = 5
list = ((2,3),1)
dict = { "rsb":"rock"}
or something like that...
I would have to look it up when I get home. I can't find it on google right now, and I know about where it is in one of my python books.
Obviously...I don't use that feature much.
"Be conservative in what you send; be liberal in what you accept." -- Postel's Law
"tacos" -- Cactus' Law
"t̥͍͎̪̪͗a̴̻̩͈͚ͨc̠o̩̙͈ͫͅs͙͎̙͊ ͔͇̫̜t͎̳̀a̜̞̗ͩc̗͍͚o̲̯̿s̖̣̤̙͌ ̖̜̈ț̰̫͓ạ̪͖̳c̲͎͕̰̯̃̈o͉ͅs̪ͪ ̜̻̖̜͕" -- -̖͚̫̙̓-̺̠͇ͤ̃ ̜̪̜ͯZ͔̗̭̞ͪA̝͈̙͖̩L͉̠̺͓G̙̞̦͖O̳̗͍
Offline
I'd forgotten about this, but I'm playing with cactus' suggestion. His code sucks. First, a = 5 should be b = 5... yeah typo... ;-)
Second, you can't have non-keyword args after keyward args, so rsb has to come at the end.
Third, other than that, cactus is, as always a genius... its exactly as he says.
To Illustrate:
>>> def function(a, b,*l,**d):
... print a
... print b
... print l
... print d
...
>>> function(4,5,(2,3),1,rsb="rock")
4
5
((2, 3), 1)
{'rsb': 'rock'}
>>> function(4,5,rsb="rock",(2,3),1)
SyntaxError: non-keyword arg after keyword arg
Dusty
Offline
Thanks guys.
I can definitely understand their worth in the link cactus gave but I don't quite get why anyone would use them in a function parameter. Must be just another or slightly easier way to do the same thing. :?
Offline
... I don't quite get why anyone would use them in a function parameter.
Ever used printf in C? Same idea as that - you can write a single function to handle both lines:
printf("Hello! %d %d %d %d", a, b, c, d); // five params
printf("Hello!"); // one param
Since I'm not much of a python guru, I'm guessing here, but the definition of an implemented printf in python would look something like this I guess:
def printf(text, *vars)
and you could pull a,b,c,d right out of vars.
Offline
you can do a C printf in python, it follows an identical syntax.
print 'today is %s the %dth' % ('wednesday',26)
today is wednesday the 26th
I just don't understand why you wouldn't create one variable in the function parameter that excepts the tuple instead of using *something which allows you to give it all the entities in the tuple which then converts it into a touple within the function. What's the point if it comes out the same way with virtually no added or lost effort?
There must be a good case for it, just can't think of any right now..
Offline
wrapper functions is a good case, suppose you write a decorater which logs every function call:
def log_call(fn):
def wrapper(*args, **kwargs):
print fn.fn_name
return fn(*args, **kwargs)
return wrapper
now you can use:
@log_call
def test(a, b, c, d):
print a,b,c,d
test(1,2,3,4)
which should produce:
test
1 2 3 4
this would not be possible without the *args, **kwargs syntax.
Offline