Function arguments

B

brasse

Hello!

Is there any way that I can get at all the arguments passed to a
function as a map without using keyword arguments?

def foo(a, b, c):
# Can I access all the arguments in a collection somewhere?

I'm mainly curious since I have stumbled on to some cases where it
might have been nice to be able to do that.

:.:: mattias
 
D

Diez B. Roggisch

brasse said:
Hello!

Is there any way that I can get at all the arguments passed to a
function as a map without using keyword arguments?

def foo(a, b, c):
# Can I access all the arguments in a collection somewhere?

I'm mainly curious since I have stumbled on to some cases where it
might have been nice to be able to do that.


There is the locals()-call that returns the local variables as dictionary.

Diez
 
C

Chris Rebert

Hello!

Is there any way that I can get at all the arguments passed to a
function as a map without using keyword arguments?

def foo(a, b, c):
# Can I access all the arguments in a collection somewhere?

You can use positional arguments:

def foo(*args):
print args

foo("a", "b", "c") #==> ["a", "b", "c"]

Though if you explained your situation more, the newsgroup could
probably be of greater help.

Cheers,
Chris
 
B

brasse

Is there any way that I can get at all the arguments passed to a
function as a map without using keyword arguments?
def foo(a, b, c):
   # Can I access all the arguments in a collection somewhere?

You can use positional arguments:

def foo(*args):
    print args

foo("a", "b", "c") #==> ["a", "b", "c"]

Though if you explained your situation more, the newsgroup could
probably be of greater help.

This is an abbreviated version of what I am doing now:

def make_data(**kw):
'''
make_data(foo='123', bar=42, time=time.time())
'''
template = '%(foo)s - %(bar)d - %(time)s'
kw['time'] = time.strftime('%c', kw['time']
return template % kw

This works, but the function signature doesn't say much about
arguments I should pass to it. What I would like to do is something
like this:

def make_data(foo, bar time):
template = '%(foo)s - %(bar)d - %(time)s'
args = magic_get_args_function()
args['time'] = time.strftime('%c', args['time']
return template % args

I hope this should clear things up a bit. :)

:.:: mattias
 
C

Chris Rebert

Is there any way that I can get at all the arguments passed to a
function as a map without using keyword arguments?
def foo(a, b, c):
# Can I access all the arguments in a collection somewhere?

You can use positional arguments:

def foo(*args):
print args

foo("a", "b", "c") #==> ["a", "b", "c"]

Though if you explained your situation more, the newsgroup could
probably be of greater help.

This is an abbreviated version of what I am doing now:

def make_data(**kw):
'''
make_data(foo='123', bar=42, time=time.time())
'''
template = '%(foo)s - %(bar)d - %(time)s'
kw['time'] = time.strftime('%c', kw['time']
return template % kw

This works, but the function signature doesn't say much about
arguments I should pass to it. What I would like to do is something
like this:

def make_data(foo, bar time):
template = '%(foo)s - %(bar)d - %(time)s'
args = magic_get_args_function()
args['time'] = time.strftime('%c', args['time']
return template % args

Just use locals() as was pointed out by Diez:

def make_data(foo, bar, time):
template = '%(foo)s - %(bar)d - %(time)s'
time = time.strftime('%c', time)
return template % locals()

Cheers,
Chris
 
B

brasse

Hello!
Is there any way that I can get at all the arguments passed to a
function as a map without using keyword arguments?
def foo(a, b, c):
   # Can I access all the arguments in a collection somewhere?
You can use positional arguments:
def foo(*args):
    print args
foo("a", "b", "c") #==> ["a", "b", "c"]
Though if you explained your situation more, the newsgroup could
probably be of greater help.
This is an abbreviated version of what I am doing now:
def make_data(**kw):
   '''
   make_data(foo='123', bar=42, time=time.time())
   '''
   template = '%(foo)s - %(bar)d - %(time)s'
   kw['time'] = time.strftime('%c', kw['time']
   return template % kw
This works, but the function signature doesn't say much about
arguments I should pass to it. What I would like to do is something
like this:
def make_data(foo, bar time):
   template = '%(foo)s - %(bar)d - %(time)s'
   args = magic_get_args_function()
   args['time'] = time.strftime('%c', args['time']
   return template % args

Just use locals() as was pointed out by Diez:

def make_data(foo, bar, time):
    template = '%(foo)s - %(bar)d - %(time)s'
    time = time.strftime('%c', time)
    return template % locals()

Nice, thank you!

:.:: mattias
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

No members online now.

Forum statistics

Threads
473,770
Messages
2,569,583
Members
45,073
Latest member
DarinCeden

Latest Threads

Top