faqts : Computers : Programming : Languages : Python : Snippets

+ Search
Add Entry AlertManage Folder Edit Entry Add page to http://del.icio.us/
Did You Find This Entry Useful?

6 of 6 people (100%) answered Yes
Recently 5 of 5 people (100%) answered Yes

Entry

Calling a function with arguments in a tuple

Jul 5th, 2000 10:00
Nathan Wallace, Hans Nowak, Snippet 140, Python FAQ


"""
Packages: faq
"""
"""
4.31. How do I call a function if I have the arguments in a tuple?
Use the built-in function apply(). For instance, 
    func(1, 2, 3)
is equivalent to 
    args = (1, 2, 3)
    apply(func, args)
Note that func(args) is not the same -- it calls func() with exactly one argument, the tuple args, instead of
three arguments, the integers 1, 2 and 3. 
"""
# Example added by PSST
if __name__ == "__main__":
    def f(a,b,c):
        return a*a*a + b*b + c
    print f(1,2,3)
    args = (1, 2, 3)
    print apply(f, args)