2

I'm a newbie to PyDev in Eclipse. When coding simple programs, I use print() statements regularly in order to track the values of variables. I want these values to be printed to the console but I couldn't get any value printed in the console so far.

Here's a simple piece of code to demonstrate what I'm trying to do.

class MyClass(object):

  def __init__(self):

    myClassObject= MyClass()
    myClassObject.greet()


    def greet(self):
       print("stackoverflow is the best !!!")

I'm expecting to see the string "stackoverflow is the best !!!" in the console but when I run the code I get nothing at all. What am I supposed to do?

Thanks in advance

Tharindu Rusira
  • 691
  • 9
  • 17

3 Answers3

2

You have not instantiated the class. In order to produce output from what you've got try the following:

if __name__ == '__main__':
  instance = MyClass()

-- REVISED --

In light of your rewritten code, you should have the following:

class MyClass(object):
    def __init__(self):
        #myClassObject= MyClass() # Causes recursion error.
        #myClassObject.greet() # You should not be accessing your instance method this way
        self.greet()


    def greet(self):
        print("stackoverflow is the best !!!")

if __name__ == '__main__':
    instance = MyClass()

Your example appears to be overly complicated. You would do well to review class usage in Python for Python 2.7 or Python 3.3.

John Percival Hackworth
  • 11,395
  • 2
  • 29
  • 38
  • `if __name__ == '__main__':` comes when I use a Module:main. But within a class I have the main method instead. (i.e. `def __init__(self):`) When I use a main module everything works fine but the problem occurs when I print() inside __init__(self) . I instantiated an object but still the console is blank. Any idea? – Tharindu Rusira Feb 12 '13 at 14:09
0

__init__ is called when the class is instantiated (cf. this question). Adding my_instance = MyClass() at the end of your snippet will create an instance of the MyClass class and should print out what you want:

class MyClass(object):

  def __init__(self):
    print("stackoverflow is the best !!!")

my_instance = MyClass()
Community
  • 1
  • 1
mtth
  • 4,671
  • 3
  • 30
  • 36
  • Are you able to `print` anything at all? Or is it just this example that doesn't work? – mtth Feb 12 '13 at 13:54
  • well I tried, unfortunately it doesn't work `class MyClass(object): def __init__(self): my_class= MyClass() my_class.greet() def greet(self): print("Hello World") ` – Tharindu Rusira Feb 12 '13 at 13:55
  • Yes @mtth , that was my worry, I couldn't print anything – Tharindu Rusira Feb 12 '13 at 13:57
  • Without the formatting and indentation, it's hard to see what is going on. I added an example, is that what you had tried? Edit - it seems to be a problem with your console then, maybe it silences print statements. – mtth Feb 12 '13 at 13:57
  • I noticed that this problem occurs only when I write a class. When I use a new main **Module:main**, it works fine and the console prints whatever written inside `if __name__ == '__main__':` – Tharindu Rusira Feb 12 '13 at 14:00
  • Does that mean you are defining your class in a separate module? If that is the case, maybe it isn't getting imported. – mtth Feb 12 '13 at 14:13
  • Yes I'm defining **MyClass** in a module named **MyClass.py**. There's nothing to import, the whole code is inside the MyClass.py module itself – Tharindu Rusira Feb 12 '13 at 14:41
0

I just learnt that my code portion in the question was meaningless because I have instantiated the objects in __init__(self). So the object has to be created inside a if __name__ == '__main__': instead.

I have written a complete module to demonstrate this. (Observe there are multiple classes in Classes.py module and then it is imported in the second part of the code)

'''
Classes.py
'''


class MyClass(object):  


    def greet(self):
        print("Hello World")


class MyNextClass(object):

        def greetAgain(self):
            print("Hello again")

'''
MyMain.py
'''
import Classes

if __name__ == '__main__':

    a=Classes.MyClass()
    a.greet()

    b=Classes.MyNextClass();
    b.greetAgain()

The above code makes sense and it will output to the console without problems.

Tharindu Rusira
  • 691
  • 9
  • 17