-1

I'm from Java background and I'm completely new in Python.

I tried to create a simple class program. The problem is that I am unable to display in method.

Code:

class Employee(object):

    def __init__(self, x, y):
        self.x=x
        self.y=y

        def display():
            print("x: ", x)
            print("y: ", y)


            #Here, I'm creating an objects..

            myObj=Employee("print 1", "print 2")
            myObj.display()

Please help.. Why I'm unable to get display? Is Something wrong in this code? I tried to create myself this code.

Thanks!

nunofmendes
  • 3,731
  • 2
  • 32
  • 42
Robot
  • 101
  • 4
  • 11
  • 1
    Indentation is used to nest blocks in Pythons. Here `display` is defined inside `__init__`. And your "main" programs is itself defined "inside" `display` . Fix indentation and you will get more interesting results. – Sylvain Leroux Feb 07 '15 at 11:16
  • Do you know any editors for it? I was using eclipse.. Please let me know sir.. – Robot Feb 07 '15 at 11:19
  • 2
    http://stackoverflow.com/questions/81584/what-ide-to-use-for-python – runDOSrun Feb 07 '15 at 11:26

1 Answers1

4

You have an indentation problem. This will fix it.

class Employee(object):

    def __init__(self, x, y):
        self.x=x
        self.y=y

    def display(self): # Have to include the `self` argument.
        print("x: ", self.x)
        print("y: ", self.y)

# Here, I'm creating an objects..
myObj=Employee("print 1", "print 2")
myObj.display()

I'm not familiar with Java, but if it's anything like most other languages, whitespace is not treated the same as in Python. Here, you really do have to take careful attention with the indentation level you're defining a function or a class in.

In your example, you'd essentially defined the display method inside the __init__ method of Employee. This would be a very strange program design choice, indeed. Most likely you didn't mean it! ;)

Edit: As someone mentioned in the comments, there is always an implicit self argument passed as the first parameter for instance methods. This parameter name isn't hardcoded. It could be rewritten to this, but it is a very strong convention. Breaking it would be senseless and is just generally not recommended. However, understanding that Python allows you to change it gives you insight into how the system works as opposed to other languages

Andrea Corbellini
  • 17,339
  • 3
  • 53
  • 69
Eithos
  • 2,421
  • 13
  • 13
  • Actually, I believe that the OP wants `display` to be a method of `Employee` and the two `myObj` lines to be at the module level. – Andrea Corbellini Feb 07 '15 at 11:19
  • Very probably the last two lines are the "main" program. So they should be unindented. – Sylvain Leroux Feb 07 '15 at 11:20
  • @AndreaCorbellini Right. I got hasty there. Thanks for the correction. – Eithos Feb 07 '15 at 11:20
  • @Eithos Okay.. your were right.. One more thing i wanted to ask.. What mostly is your choice personally for python(Good editor?) .. – Robot Feb 07 '15 at 11:25
  • @Robot I use Sublime Text personally... I haven't experimented much with other editors. – Eithos Feb 07 '15 at 11:26
  • @Eithos okay.. one more thing i wanted to ask.. I'm getting undefined variable inside `display` method.. Can you please help me .. What's wrong here? I'm new in python.. But python ***says we don't need to define variable***, Then why i'm getting this error? and yes i'd accept your answer after `2 minutes`. – Robot Feb 07 '15 at 11:29
  • @Robot Oh, yep. @Andrea Corbellini fixed that. You have to call it with `self` before the attribute name. Remember, `self` is a reference to the object, and the attributes you set in `__init__` belong only to that object. – Eithos Feb 07 '15 at 11:31
  • Thank you dude... :) Next time .. I'll remember – Robot Feb 07 '15 at 11:34