5

I have a requirement that I have to write two functions with the same name in Python. How should I do it?

class QueueCards:
    def __init__(self):
        self.cards = []

    def Add(self, other):
        self.cards.insert(0, other)

    def Add(self, listCards, numCards):
        for i in numCards:
            card = listCards.GetCard(i)
            self.Add(card)

Count() is the size of the queue.

Peter Mortensen
  • 30,738
  • 21
  • 105
  • 131
rotemhas
  • 71
  • 2
  • 8

3 Answers3

11

You can't do it. At least, not in the same namespace (i.e.: same module, or same class). It seems you are trying to do something you've learned in one language and are trying to apply it to Python.

Instead, you can have Add take a variable number of arguments, so you can do different things depending on what was passed in.

def Add(self, *args):
    if len(args) == 1:
        item = args[0]
        self.cards.insert(0, item)

    elif len(args) == 2):
        listCards, numCards = args
        for i in numCards:
            card = listCards.GetCard(i)
            self.cards.insert(0, card)

Personally I think it is better to have two functions because it avoids ambiguity and aids readability. For example, AddCard and AddMultipleCards.

Or, perhaps even better, a single function that you use for any number of cards. For example, you can define Add to take a list of cards, and you add them all:

def Add(self, *args):
    for card in args:
        self.cards.insert(0, card)

Then, call it with either a single card:

self.Add(listCards.GetCard(0))

... or, a list of cards:

list_of_cards = [listCards.GetCard(i) for i in range(len(listCards))]
self.Add(*list_of_cards)

You appear to be asked to do function overloading, which is simply not something that Python supports. For more information about function overloading in Python, see this question: Python function overloading

Peter Mortensen
  • 30,738
  • 21
  • 105
  • 131
Bryan Oakley
  • 370,779
  • 53
  • 539
  • 685
4

In Python 2.7, for some reason, you can do this:


def xyz():
    print "R"
def xyz():
    print "T"
xyz()

If you were to run this, the result would be "T" so the second (or last) version of the function would be called.

I found this out by accident and it took me ages to find out why my program wasn't working!

Peter Mortensen
  • 30,738
  • 21
  • 105
  • 131
joe_evans
  • 120
  • 5
1

Based on the fact it is a requirement you have to fulfill, if you can use a function you can use the function with the same name in the method i.e:

def Add(cards, item):
    cards.insert(0, item)

class QueueCards:
    def __init__(self):
        self.cards = []
    def Add(self, listCards, numCards):
        for i in [1, 2, 3]:
            Add(self.cards, 4)



q = QueueCards()

q.Add(q.cards, 4)
print(q.cards)

[4, 4, 4]

Or you can also pass the instance and access the list through the instance:

def Add(inst, item):
    inst.cards.insert(0, item)

class QueueCards:
    def __init__(self):
        self.cards = []

    def Add(self, listCards, numCards):
        for i in [1, 2, 3]:
           Add(self, numCards)


q = QueueCards()

q.Add(q.cards, 4)
print(q.cards)

Which would give you the same output. I could not follow the logic of your methods but the idea will be the same whatever you methods actually do.

Padraic Cunningham
  • 176,452
  • 29
  • 245
  • 321