NameError: global name 'self' not defined -Classes

I try to learn classes, and something holds them back, I get

"NameError: global name 'self' is not defined" 

the same thing happens with every field in the class. you can help me find what I'm doing wrong, thanks

code:

 class Assignment: def __init__(self, name, discription, deadline, grade, studentID): self.name = name self.studentID = studentID self.description = discription self.deadline = deadline self.grade = grade def __str__(self): return "studentID:" + self.studentID + "assignment name:" + self.name +" description:" + self.description + " deadline:" + self.deadline + " grade:" + self.grade def validation(self): errors= [] if self.studendID == "": errors.append("No existing student ID.") if self.description == "": errors.append("No existing description.") if self.deadline == "": errors.append("No existing deadline.") if self.deadline == "": errors.append("No existing deadline.") return errors @staticmethod def dummyAssignments(): ret = [] for studentID in range(100, 121): print "sda" a = Assignment(self, name, discription, deadline, grade, studentID) ret.append(a) return ret def testAssigment(): a = Assignment("","","","","") print a testAssigment() print Assignment.dummyAssignments() 
+4
source share
4 answers

You do not need to pass self when instantiating the class.

 Assignment(self, name, discription, deadline, grade, studentID) 

it should be

 Assignment(name, discription, deadline, grade, studentID) 

The error lets you know that you are trying to use var self , which is not defined in a local or global scope.

+3
source

The problem is here:

 a = Assignment(self, name, discription, deadline, grade, studentID) 

This is in @staticmethod , so self not defined.

Indeed, none of these values ​​are defined, think about it - except studentID .

+6
source

The dummyAssignments static method has only studentIDs, but not any other fields.

Try specifying default values ​​for each of the fields:

 class Assignment: def __init__(self, name='', discription='', deadline='', grade='', studentID =''): self.name = name self.studentID = studentID self.description = discription self.deadline = deadline self.grade = grade def __str__(self): return "studentID:" + self.studentID + "assignment name:" + self.name +" description:" + self.description + " deadline:" + self.deadline + " grade:" + self.grade def validation(self): errors= [] if self.studendID == "": errors.append("No existing student ID.") if self.description == "": errors.append("No existing description.") if self.deadline == "": errors.append("No existing deadline.") if self.deadline == "": errors.append("No existing deadline.") return errors @staticmethod def dummyAssignments(): ret = [] for studentID in range(100, 121): print "sda" a = Assignment(studentID=studentID) ret.append(a) return ret def testAssigment(): a = Assignment("","","","","") print a testAssigment() print Assignment.dummyAssignments() 
+2
source

in your class:

 class Assignment: 

change it to

 class Assignment(): 

or

 class Assignment(object): 
0
source

Source: https://habr.com/ru/post/1381023/


All Articles