Your actual problem is that you are defining your instance method greet
as:
def greet(name, other_name):
Instance methods in Python take the instance
as first argument in your method definitions. What you are doing here now, is calling that instance name
. However, in your class's __init__
, you are using self
to refer to your instance object. This is where your main problem is coming up.
So, when you actually try to call your method, you are most likely getting something like this:
Hi bob, my name is <__main__.Person object at 0x1018ff2b0>
So, you are actually printing out your instance object of your Person
class. Again, you named this name
. It's like printing out self
.
There are two things you need to correct here. The first, you need to properly define your instance greet
method keeping your instance object name consistent.
def greet(self, other_name):
Then, you need to refer to your instance attributes referring to your instance and accessing the attributes from that object:
So, you want to access name
in your greet
method, it has to be as self.name
.
So:
"Hi {0}, my name is {1}".format(other_name, self.name)
To have a better grasp on all this, you should read more on how classes work in Python. Here is the tutorial section on it:
https://docs.python.org/3/tutorial/classes.html