Reputation: 11059
New to python.... really confused with this weird error.... I don't think I'm doing anything wrong, am I?
main.py
from methods import Reverse
def main ():
b = [1,2,3,4,5,6,6,7,8,8,9,1,212,312,31,23,123455435,56,56,123]
a = "llaa"
thing = Reverse()
print thing.reverse(a)
main()
methods.py
class Reverse():
def __init__(self):
print "initialized reverse"
def reverse(self,var):
if var == "":
return var
else:
print var[-1] + reverse(var[:-1])
I'm trying to do a recursive call with reverse.... Please kindly help. Thanks!
Upvotes: 1
Views: 5822
Reputation: 8805
Unlike some other languages where this
is implied, Python requires you to explicitly say self
whenever using instance methods or variables so in Reverse
you need to explicitly say
self.reverse(var[:-1])
Of course reverse
does not return anything so you need to change it to
return var[-1] + self.reverse(var[:-1])
Upvotes: 4
Reputation: 388123
In your class Reverse
, reverse
is a method, so you need to call it as a method:
print var[-1] + self.reverse(var[:-1])
Note the self
.
Upvotes: 4