Python Forum
error in class - Printable Version

+- Python Forum (https://python-forum.io)
+-- Forum: Python Coding (https://python-forum.io/forum-7.html)
+--- Forum: General Coding Help (https://python-forum.io/forum-8.html)
+--- Thread: error in class (/thread-29446.html)



error in class - non_name092 - Sep-02-2020

class Student():
    def __init__(self, name, CNE, Level ):
        self.name=name
        self.CNE=CNE
        self.Level=Level
    def student_name(self):
        student_info= "name is :" +  self.name + ",CNE is :" + str(self.CNE) + " and Level is :" + self.Level
        return student_info
Student("mohammad ", 1123456789," PhD Student")
print(Student.student_name())
Error:
Error:
Traceback (most recent call last): File "C:/Users/HP/Desktop/Telegram Desktop/class practice.py", line 10, in <module> print(Student.student_name()) TypeError: student_name() missing 1 required positional argument: 'self' Process finished with exit code 1



RE: error in class - bowlofred - Sep-02-2020

student_name() is set up as a class method. It shouldn't be called directly as you do on line 10. Instead, you should be capturing the instance created on line 9 and use that to call the method.

...
mystudent = Student("mohammad ", 1123456789," PhD Student")
print(mystudent.student_name())
Output:
name is :mohammad ,CNE is :1123456789 and Level is : PhD Student