31

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()
3
  • 2
    why do you need to define self? Commented Jul 24, 2013 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. Commented Jul 24, 2013 at 20:09
  • @BenHoyt: I suppose you meant 'useful' ;) Commented Aug 24, 2014 at 11:32

3 Answers 3

33

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, ....)
Sign up to request clarification or add additional context in comments.

8 Comments

This seem like it would be slowing down the process a lot though, does it?
@Rabbitybunny What do you mean slowing down?
@Rabbitybunny What do you mean? How would it slow anything down to create methods in the only way possible?
Oh, okay, I just thinking that always calling a class might be a slow process. Thanks again.
@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.
|
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)   

1 Comment

yet an explanation why it works would come handy
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 recursion: ", 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("Recursion using class: ", result.recur())

1 Comment

This needs self.recur(self.number-1) as the recursive call.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.