Note that there are some explanatory texts on larger screens.

plurals
  1. PO
    primarykey
    data
    text
    <p>Python does support "method overloading" as you present it. In fact, what you just describe is trivial to implement in Python, in so many different ways, but I would go with:</p> <pre><code>class Character(object): # your character __init__ and other methods go here def add_bullet(self, sprite=default, start=default, direction=default, speed=default, accel=default, curve=default): # do stuff with your arguments </code></pre> <p>In the above code, <code>default</code> is a plausible default value for those arguments, or <code>None</code>. You can then call the method with only the arguments you are interested in, and Python will use the default values. </p> <p>You could also do something like this:</p> <pre><code>class Character(object): # your character __init__ and other methods go here def add_bullet(self, **kwargs): # here you can unpack kwargs as (key, values) and # do stuff with them, and use some global dictionary # to provide default values and ensure that ``key`` # is a valid argument... # do stuff with your arguments </code></pre> <p>Another alternative is to directly hook the desired function directly to the class or instance:</p> <pre><code>def some_implementation(self, arg1, arg2, arg3): # implementation my_class.add_bullet = some_implementation_of_add_bullet </code></pre> <p>Yet another way is to use an abstract factory pattern:</p> <pre><code>class Character(object): def __init__(self, bfactory, *args, **kwargs): self.bfactory = bfactory def add_bullet(self): sprite = self.bfactory.sprite() speed = self.bfactory.speed() # do stuff with your sprite and speed class pretty_and_fast_factory(object): def sprite(self): return pretty_sprite def speed(self): return 10000000000.0 my_character = Character(pretty_and_fast_factory(), a1, a2, kw1=v1, kw2=v2) my_character.add_bullet() # uses pretty_and_fast_factory # now, if you have another factory called "ugly_and_slow_factory" # you can change it at runtime in python by issuing my_character.bfactory = ugly_and_slow_factory() # In the last example you can see abstract factory and "method # overloading" (as you call it) in action </code></pre>
    singulars
    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.
    1. This table or related slice is empty.
    1. VO
      singulars
      1. This table or related slice is empty.
    2. VO
      singulars
      1. This table or related slice is empty.
    3. VO
      singulars
      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