0

what is wrong with this program.i m getting name error:

NameError: name 'sum1' is not defined

 class sum:
    def sum1(self,a,b):
       self.c=a+b
       return self.c
    def init(self,a,b):
       self.a=a
       self.b=b
       return sum1(self.a,self.b)

 d=sum()
 a=6
 b=7
 h=d.init(a,b)
 print(h)
bharat
  • 1,762
  • 1
  • 24
  • 32
  • Use self.sum1 . sum1 is not in scope of init. But, is bound as a method on self (which is the instance itself) – Vasif Feb 02 '18 at 02:10
  • 2
    Possible duplicate of [Python call function within class](https://stackoverflow.com/questions/5615648/python-call-function-within-class) – xskxzr Feb 02 '18 at 03:57

2 Answers2

0

You have an error on return statement.

return self.sum1(self.a, self.b)
Vasif
  • 1,393
  • 10
  • 26
0

As long as you're within a class, it has to be:

class sum:
    def sum1(self,a,b):
        self.c=a+b
        return self.c

   def init(self,a,b):
       self.a=a
       self.b=b
       return self.sum1(self.a,self.b)

d=sum()
a=6
b=7
h=d.init(a,b)
print(h)

You did not prefix the sum1 call within the class with self:

return self.sum1(self.a,self.b)
dmitryro
  • 3,463
  • 2
  • 20
  • 28