1

So I've been self-learning Python, and creating Pygame ("Alien Invasion") in the book. While I was following step-by-step explanation, I realized the author has a certain order of parameters in every functions. For example,

def check_bullet_alien_collisions(ai_settings, screen, stats, sb, ship, 
    aliens, bullets):
    """Respond to bullet-alien collisions."""
    # Remove any bullets and aliens that have collided.
    collisions = pygame.sprite.groupcollide(bullets, aliens, True, True)
    if collisions:
        for aliens in collisions.values():
            stats.score += ai_settings.alien_points * len(aliens)
            sb.prep_score()

I assumed that these parameters in this function works simultaneously, so the order didn't matter much, but obviously it does matter. But how do I figure out which comes first and last? How do I decide which should be first and last?

3
  • Non-keyword arguments (aka positional arguments) correspond to their names in a function definition via order.
    – Andrew Li
    Commented Dec 17, 2018 at 22:16
  • If you Google the phrase "Python function parameter order", you’ll find resources that can explain it much better than we can in an answer here.
    – Prune
    Commented Dec 17, 2018 at 22:17
  • Thanks I'll actually google it!!!! I was only searching in stackoverflow, and didn't find any answer that satisfied me...
    – Sarah
    Commented Dec 17, 2018 at 22:31

3 Answers 3

8

In most other languages, such as C or C++, when you pass arguments you pass them in order.

For example:

int my_function(string name, int size, char type)
{
    ...
}

int main()
{
    my_function("item1", 10, 'c')
}

This is how the compiler actually organizes the code in real machine code.

In Python, they make things a bit higher level for programmers, so you can do something like this

def my_function(name="default", size=1, type='c'):
    ...

my_function(size=4, name="item1", type=d)

Notice how you can call the function with arguments in a different order than the function was defined.

The only way to know what value you meant to pass for each parameter is by using the correct order. For python, you can get around this by using the assignment operator to the parameters in the call (such as size=2).

Once you do a call in a different order than the way the function was defined, you have to explicitly state which parameter you are assigning for all the following arguments. This makes sense since you have now called the function out of order and it needs to know which parameters you are referring to.

For instance, you can do this:

def my_function(name, size, type, id):
    ...

my_function("item1", size=4, type=d, id=3)

but you cannot do this:

my_function("item1", size=4, d, 3)

Since it now doesn't know if you mean to set d for the id or d for the type and vice versa.


Your question was how to know what order the function would like the arugments in. The best way to find this is to read the documentation for the function you are using.

If you go here https://www.pygame.org/docs/ref/sprite.html#pygame.sprite.groupcollide

You can find pygame.sprite.groupcollide and it gives you the function arguments in the order it expects. You could also manually assign then with the = operator after each parameter.

#documentation found online
pygame.sprite.groupcollide()
Find all sprites that collide between two groups.
groupcollide(group1, group2, dokill1, dokill2, collided = None)

For example in your code you can call:

  pygame.sprite.groupcollide(group1=bullets, group2=aliens, dokill1=True, dokill2=True, collided = None)
2
  • Ordering isn't always so complex: def func(a, b): vs def func(b, a) is a matter of readability and personal preference, unless they cause side effects for eachother. Commented Dec 17, 2018 at 22:35
  • Thank you so much for detailed answer. I understand clearly now!
    – Sarah
    Commented Dec 19, 2018 at 18:29
1

The simplest way to deal with a long parameter list is to name the individual parameters rather than relying on the order:

check_bullet_alien_collisions(ai_settings=AiSettings, screen=Screen, stats=Stats, sb=SB, ship=Ship, aliens=Aliens, bullets=Bullets)

Now you can see what the values are for each parameter without knowing the order.

0
0

Use wildcard parameters and handle them dynamically.

def check_bullet_alien_collisions(*args, **kwargs):
    ai_settings = args[0]
    screen = kwargs.get('screen')

This also allows you to pass through parameters for other functions if you need to.

http://book.pythontips.com/en/latest/args_and_kwargs.html

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Not the answer you're looking for? Browse other questions tagged or ask your own question.