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