Note that there are some explanatory texts on larger screens.

plurals
  1. POPython function overloading
    primarykey
    data
    text
    <p>I know that Python does not support method overloading, but I've run into a problem that I can't seem to solve in a nice Pythonic way.</p> <p>I am making a game where a character needs to shoot a variety of bullets, but how do I write different functions for creating these bullets? For example suppose I have a function that creates a bullet travelling from point A to B with a given speed. I would write a function like this:</p> <pre><code> def add_bullet(sprite, start, headto, speed): ... Code ... </code></pre> <p>But I want to write other functions for creating bullets like:</p> <pre><code> def add_bullet(sprite, start, direction, speed): def add_bullet(sprite, start, headto, spead, acceleration): def add_bullet(sprite, script): # For bullets that are controlled by a script def add_bullet(sprite, curve, speed): # for bullets with curved paths ... And so on ... </code></pre> <p>And so on with many variations. Is there a better way to do it without using so many keyword arguments cause its getting kinda ugly fast. Renaming each function is pretty bad too because you get either <code>add_bullet1</code>, <code>add_bullet2</code>, or <code>add_bullet_with_really_long_name</code>.</p> <p>To address some answers:</p> <ol> <li><p>No I can't create a Bullet class hierarchy because thats too slow. The actual code for managing bullets is in C and my functions are wrappers around C API.</p></li> <li><p>I know about the keyword arguments but checking for all sorts of combinations of parameters is getting annoying, but default arguments help allot like <code>acceleration=0</code></p></li> </ol>
    singulars
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    plurals
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    1. This table or related slice is empty.
 

Querying!

 
Guidance

SQuiL has stopped working due to an internal error.

If you are curious you may find further information in the browser console, which is accessible through the devtools (F12).

Reload