stackken
stackken

Reputation: 63

python function, passing multi arguments and don't use them all?

I just started python and am trying to wrap my head around this concept of having arguments in a function but not using all of them, would like to know the logic behind this.

The code looks like this:

def player_busts(player,dealer,chips):
    print("Player busts!")
    chips.lose_bet()

It's meant to take three arguments: a player object, a dealer object, and a chips object. But why they only used the chips object passed in? Do other two play any role here?

Thank you very much.

Upvotes: 1

Views: 1598

Answers (3)

e.s.
e.s.

Reputation: 1371

This often happens with callback functions. A callback is a function will get called with certain pre-set parameters when a specified event happens.

Somewhere in a larger program, is another game-playing function that calls these.

def play():
    player = ???()
    dealer = ???()
    chips = ???()

    while something:
        if another_thing:
            action = player_wins
        elif something_else:
            action = player_busts
        elif another_thing:
            action = dealer_busts
        else:
            action = push

        action(player, dealer, chips)

Since some of these actions need to use player or dealer or chips or any combination of them, any action called would need to accept those parameters even if it doesn't use them.

If you defined player_busts like so,

def player_busts(chips):
    print("Player busts!")
    chips.lose_bet()

You'd get the following error if it was called in play. TypeError: player_busts takes exactly one argument (3 given)

Upvotes: 1

pyeR_biz
pyeR_biz

Reputation: 1044

This is not a complete code sample, specially if your instructor has provided you with it. More functionality will be added to it as the course goes on, to show you how a full program is built step by step. You have most probably jumped the gun and posted this partial code here.

Firstly, The function is not returning anything, which means it is evaluating something and it stops there, the result of evaluation cannot be used anywhere. Which basically means more statement will be added to this function till your reach the return statement.

Secondly, chips.lose_bet() is a statement calling a function .lose_bet() which is also not defined in this sample.

Third, the arguments in this function might as well be functions themselves that will be defined later; and when they will be defined the instructor, they may be called from this function. There are many possibilities in ways similar to this.

These few points are bigger holes in the above code than having extra arguments. Which basically means you have actually jumped the gun in trying to understand what is going on here. So continue with your course and you'll see.

Upvotes: 0

PydPiper
PydPiper

Reputation: 498

A function can take as many argument as you like to feed it, however not using it with a function is a waste. The purpose of a function is to take in arguments and do some work with them to return a argument or arguments back. In your example only chips are used therefore the other two can be removed and still have the function behave exactly the same.

Upvotes: 0

Related Questions