1

I can not seem to find an answer for this question on the interwebs. In Python, am I able to do something like this:

class MyClass(object):
    """It's my class, yo!"""
    def __init__(self, string_of_interest_1):
        self.string1 = string_of_interest_1
        self.string2 = None
        self.int1 = None
    def __init__(self, string_of_interest_1, string_of_interest2):
        self.string1 = string_of_interest_1
        self.string2 = string_of_interest_2
        self.int1 = None
    def __init__(self, int_of_interest_1):
        self.string1 = None
        self.string2 = None
        self.int1 = int_of_interest_1

I call this "method-overloading", but it seems the standard overloading is when I have some variables that I assign a default value to within the call itself. (E.g., def __init__(self, string_of_interest_1, string_of_interest_2 = None)) While this tiny example does not demonstrate it, I would like the potential to have truly different methods depending upon how many, and what type, of parameters are passed to it.

Thanks!

kylieCatt
  • 10,672
  • 5
  • 43
  • 51
Mike Williamson
  • 4,915
  • 14
  • 67
  • 104
  • 5
    No, this doesn't work, you just replace the previous definition. However, you can have variable number of arguments and optional argument/keywords to do the same thing. – mdurant Aug 05 '14 at 16:10
  • 1
    Plus, in python you never presume the type of an input variable - it simply needs to be able to do the things required of it, see "duck typing". – mdurant Aug 05 '14 at 16:11
  • 1
    Python will make no distinction between `string_of_interest_1` and `int_of_interest_1`. It will not do any type checking at that time. – TheSoundDefense Aug 05 '14 at 16:11
  • 1
    You might be interested in [multimethods](http://www.artima.com/weblogs/viewpost.jsp?thread=101605) and [PEAK-Rules](https://pypi.python.org/pypi/PEAK-Rules). `multimethods` will allow you to dispatch based on the type of arguments. `PEAK-Rules` will allow you to dispatch on the type and value of arguments. I'm not sure what facilities it may or may not have for dispatching based on the number of arguments. – unutbu Aug 05 '14 at 16:36

3 Answers3

2

In python there is no overloading of functions or methods, but there is a change - *args and **kwargs. Example:

>>def test(*args, **kwargs):
>>    print args # list of positional parametrs
>>    print kwargs # dict of named parametrs

>>test(1, 2, test_param='t')
[1, 2]
{test_param: 't'}
Alexander Zh
  • 111
  • 3
1

All variable in Python is object even int is also a object.

Method overriding used in C, JAVA etc..

Python has magic variable * and **. * is used for position argument, and ** used as keyword argument

try this

def test_method(*args, **kwargs):
    print args
    print kwargs

Call this function with different different argument and you will get idea about these magic methods.

Nilesh
  • 20,521
  • 16
  • 92
  • 148
1

You can take all the arguments that you want even when your function doesn't know the number. The syntax is to use the symbol * to take in a variable number of arguments; by convention, it is often used with the word args. What *args allows you to do is take in more arguments than the normal of formal arguments that you previously defined. Check this thread to see an example of how you can do it

*args and **kwargs?

Community
  • 1
  • 1
Juan David
  • 2,676
  • 4
  • 32
  • 42