Yugantar
Yugantar

Reputation: 21

Calling a function into main declared inside one class, but unable to

I am calling a function - kindle into main function both declared inside a class, and i am instantiating the object of the class inside main and then trying to access kindle with it, but i am unable to. The error shows as : NameError: name 'Program' is not defined.

class Program:

   def kindle():
      x = 2
      return x

   def main():
      p = Program()
      p.kindle()
   if __name__ == "__main__":
      main()

Upvotes: 0

Views: 39

Answers (1)

Mike
Mike

Reputation: 1164

Since main() calling Program, the source looks incorrect. It should be

class Program:

   def kindle():  # bad
      x = 2
      return x

def main():
   p = Program()
   p.kindle()

if __name__ == "__main__":
   main()

Also kindle definition should be either

@staticmethod
def kindle():

or def kindle(self):

Upvotes: 1

Related Questions