28

I just learn python today, and so am thinking about writing a code about recursion, naively. So how can we achieve the following in python?

class mine:
    def inclass(self):
        self = mine();
    def recur(num):
        print(num, end="")
        if num > 1:
            print(" * ",end="")
            return num * self.recur(num-1)
        print(" =")
        return 1

def main():
    a = mine()
    print(mine.recur(10))

main()

I tried to define self, but could not think of a way to do so. Any suggestions? Thank you very much.


Yes the following work, thanks.

class mine:
    def recur(self, num):
        print(num, end="")
        if num > 1:
            print(" * ",end="")
            return num * self.recur(self, num-1)
        print(" =")
        return 1

def main():
    a = mine()
    print(mine.recur(mine, 10))

main()
MattDMo
  • 100,794
  • 21
  • 241
  • 231
Rabbitybunny
  • 337
  • 1
  • 3
  • 7
  • 2
    why do you need to define self? – Serial Jul 24 '13 at 20:03
  • Two notes: 1) the `inclass` function doesn't do anything useless, it just assigns an instance of `mine` to the local name called `self` and then throws it away, and 2) I don't see any real reason to make this a class -- just a plain `recur()` function would do. – Ben Hoyt Jul 24 '13 at 20:09
  • @BenHoyt: I suppose you meant 'useful' ;) – Niroshan Aug 24 '14 at 11:32

3 Answers3

26

Each method of a class has to have self as a first parameter, i.e. do this:

def recur(self, num):

and it should work now.

Basically what happens behind the scene is when you do

instance.method(arg1, arg2, arg3, ...)

Python does

Class.method(instance, arg1, arg2, arg3, ....)
Community
  • 1
  • 1
freakish
  • 54,167
  • 9
  • 132
  • 169
  • This seem like it would be slowing down the process a lot though, does it? – Rabbitybunny Jul 24 '13 at 20:15
  • @Rabbitybunny What do you mean slowing down? – freakish Jul 24 '13 at 20:16
  • @Rabbitybunny What do you mean? How would it slow anything down to create methods in the only way possible? – Marcin Jul 24 '13 at 20:16
  • Oh, okay, I just thinking that always calling a class might be a slow process. Thanks again. – Rabbitybunny Jul 24 '13 at 20:19
  • @Rabbitybunny Well, first of all: it is Python. Thus these kind of performance issues should not be on your mind at all - performance is not why people use Python. Secondly: it's not really a big deal, getting a class/method from memory is extremely fast. You will find out that hardly anyone does these kind of performance upgrades, because it is time and money consuming and you won't even see the difference. – freakish Jul 24 '13 at 20:21
  • @Rabbitybunny In no way is this "calling a class". – Marcin Jul 24 '13 at 20:26
  • @Marcin It does have to do a lookup for a method in a class. I assume that this is what he means. – freakish Jul 24 '13 at 20:28
  • Actually, you could also use a staticmethod if you don't need self. – skywalker Jun 10 '16 at 17:39
0

This is a code example that actually works

class Card():
    def __init__(self,cardsPlayedList,cardPosition):
        self.cardsPlayedList = cardsPlayedList
        self.cardPosition = cardPosition
    #    self.cardPosition

    def getNewCard(self,cardPosition):
        cardNum = 0
        cardInList = False
        
        cardNum = random.randint(1,len(cardList)-1)          # Get random card from List - 1 to 52 
        for x in self.cardsPlayedList:
            if(cardNum == x):
                cardInList = True                            

        if(cardInList == False):
            self.cardsPlayedList.insert(self.cardPosition, cardNum)    # if card not already played then store in list
            return cardNum
        else:
            return self.getNewCard(cardPosition)   
Ruli
  • 2,592
  • 12
  • 30
  • 40
0
# USING FUNCTION    
def function_recursion(number):
    if number <= 1:
        return number
    else:
        return (number + function_recursion(number - 1))
result = function_recursion(5)
print("function recurssion: ", result)

#USING CLASS
class Recursion:
    def __init__(self, number):
        self.number = number
        
    def recur(self):
        if self.number <= 1:
            return True
        else:
            return (self.number + self.recur(self.number - 1))
result = Recursion(3)
print("Recurssion using class: ",result.recur())
Abayomi Olowu
  • 199
  • 1
  • 10