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