You are not logged in.

#1 2006-07-25 21:40:56

T-Dawg
Forum Fellow
From: Charlotte, NC
Registered: 2005-01-29
Posts: 2,736

python def foo(*bla, **blab) ?? [solved]

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

#2 2006-07-25 22:10:57

cactus
Taco Eater
From: t͈̫̹ͨa͖͕͎̱͈ͨ͆ć̥̖̝o̫̫̼s͈̭̱̞͍̃!̰
Registered: 2004-05-25
Posts: 4,622
Website

Re: python def foo(*bla, **blab) ?? [solved]

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. wink


"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

#3 2006-07-25 23:22:02

Dusty
Schwag Merchant
From: Medicine Hat, Alberta, Canada
Registered: 2004-01-18
Posts: 5,986
Website

Re: python def foo(*bla, **blab) ?? [solved]

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

#4 2006-07-25 23:50:20

T-Dawg
Forum Fellow
From: Charlotte, NC
Registered: 2005-01-29
Posts: 2,736

Re: python def foo(*bla, **blab) ?? [solved]

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

#5 2006-07-26 02:40:22

Cerebral
Forum Fellow
From: Waterloo, ON, CA
Registered: 2005-04-08
Posts: 3,108
Website

Re: python def foo(*bla, **blab) ?? [solved]

Penguin wrote:

... 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

#6 2006-07-26 10:58:16

T-Dawg
Forum Fellow
From: Charlotte, NC
Registered: 2005-01-29
Posts: 2,736

Re: python def foo(*bla, **blab) ?? [solved]

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

#7 2006-07-26 12:43:52

cmp
Member
Registered: 2005-01-03
Posts: 350

Re: python def foo(*bla, **blab) ?? [solved]

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

Board footer

Powered by FluxBB