Get started learning Python with DataCamp's free Intro to Python tutorial. Learn Data Science by completing interactive coding challenges and watching videos by expert instructors. Start Now!

This site is generously supported by DataCamp. DataCamp offers online interactive Python Tutorials for Data Science. Join 11 million other learners and get started learning Python for data science today!

Good news! You can save 25% off your Datacamp annual subscription with the code LEARNPYTHON23ALE25 - Click here to redeem your discount

Több függvényargumentum


Minden Python függvény előre meghatározott számú argumentumot kap, ha normál módon van deklarálva, például így:

def myfunction(first, second, third):
    # csinálj valamit a 3 változóval
    ...

Lehetőség van olyan függvények deklarálására is, amelyek változó számú argumentumot kapnak, a következő szintaxissal:

def foo(first, second, third, *therest):
    print("First: %s" % first)
    print("Second: %s" % second)
    print("Third: %s" % third)
    print("And all the rest... %s" % list(therest))

A "therest" változó egy listája azoknak a változóknak, amelyek megkapják a "foo" függvénynek átadott összes argumentumot az első 3 argumentum után. Tehát a foo(1, 2, 3, 4, 5) hívás a következőt fogja kiírni:

def foo(first, second, third, *therest):
    print("First: %s" %(first))
    print("Second: %s" %(second))
    print("Third: %s" %(third))
    print("And all the rest... %s" %(list(therest)))

foo(1, 2, 3, 4, 5)

Lehetőség van kulcsszóval ellátott argumentumok küldésére is a függvényeknek, így az argumentumok sorrendje nem számít, a következő szintaxissal. Az alábbi kód a következő kimenetet eredményezi: The sum is: 6 Result: 1

def bar(first, second, third, **options):
    if options.get("action") == "sum":
        print("The sum is: %d" %(first + second + third))

    if options.get("number") == "first":
        return first

result = bar(1, 2, 3, action = "sum", number = "first")
print("Result: %d" %(result))

A "bar" függvény 3 argumentumot fogad. Ha kiegészítő "action" argumentum érkezik, amely az utasítást adja a számok összegzésére, akkor az összeg ki lesz írva. Alternatív megoldásként a függvény azt is tudja, hogy vissza kell adnia az első argumentumot, ha a függvénybe átadott "number" paraméter értéke "first".

Exercise

Egészítsd ki a foo és bar függvényeket, hogy változó számú argumentumot tudjanak fogadni (3 vagy több). A foo függvénynek vissza kell adnia a plusz paraméterek számát. A bar akkor térjen vissza True értékkel, ha a magicnumber kulcsszóval ellátott argumentum értéke 7, ellenkező esetben False.

# edit the functions prototype and implementation def foo(a, b, c): pass def bar(a, b, c): pass # test code if foo(1, 2, 3, 4) == 1: print("Good.") if foo(1, 2, 3, 4, 5) == 2: print("Better.") if bar(1, 2, 3, magicnumber=6) == False: print("Great.") if bar(1, 2, 3, magicnumber=7) == True: print("Awesome!") # edit the functions prototype and implementation def foo(a, b, c, *args): return len(args) def bar(a, b, c, **kwargs): return kwargs["magicnumber"] == 7 # test code if foo(1, 2, 3, 4) == 1: print("Good.") if foo(1, 2, 3, 4, 5) == 2: print("Better.") if bar(1, 2, 3, magicnumber=6) == False: print("Great.") if bar(1, 2, 3, magicnumber=7) == True: print("Awesome!") test_output_contains("Good.") test_output_contains("Better.") test_output_contains("Great.") test_output_contains("Awesome!") success_msg("Great work!")

This site is generously supported by DataCamp. DataCamp offers online interactive Python Tutorials for Data Science. Join over a million other learners and get started learning Python for data science today!

Previous Tutorial Next Tutorial Take the Test
Copyright © learnpython.org. Read our Terms of Use and Privacy Policy