This is an assignment. But it don’t work.
class Student:
def __init__(self, name):
self.name = name
def greet():
print(self.name+" says hi")
obj = Student("John")
greet(obj)
It is to output “John say hi”
Thanks,
This is an assignment. But it don’t work.
class Student:
def __init__(self, name):
self.name = name
def greet():
print(self.name+" says hi")
obj = Student("John")
greet(obj)
It is to output “John say hi”
Thanks,
I found the answer!!! one line needed to define self. def greet(self): The self was missing.
Do you think I will forget this? NO. LOL
Oh, I beg to differ. I have been programming in Python for years, I have a snazzy Python-aware editor which automatically completes what I type, and I still make this mistake.
Welcome to Python!
Also greet()
is a method so you need to call it a different way:
a) obj.greet()
- The normal way of calling methods.
b) Student.greet(obj)
- This way is normally used in different cases - when you need to get the function object (Student.greet
).