Function arguments

brasse thebrasse at gmail.com
Mon Jan 26 15:49:25 CET 2009


On Jan 26, 10:39 am, Chris Rebert <c... at rebertia.com> wrote:
> On Mon, Jan 26, 2009 at 1:34 AM, brasse <thebra... at gmail.com> wrote:
> > On Jan 26, 10:11 am, Chris Rebert <c... at rebertia.com> wrote:
> >> On Mon, Jan 26, 2009 at 1:03 AM, brasse <thebra... at gmail.com> wrote:
> >> > 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



More information about the Python-list mailing list