Im trying to make a method that checks if the object will be of a faculty type based from the faculty class. This is the code but im getting a syntax error?
def addFaculty(self, f_obj):
if f_obj isinstance(f_obj, Faculty):
return True
else:
self.directory.append(f_obj)
CodePudding user response:
Try this:
def addFaculty(self, f_obj):
return isinstance(f_obj, Faculty)
or:
def addFaculty(self, f_obj):
return type(f_obj) is Faculty
CodePudding user response:
Class name not acceptable to use from method scope.
For this purposes use __class__
attr of obj self
.
def addFaculty(self, f_obj):
if isinstance(f_obj, self.__class__):
return True
else:
self.directory.append(f_obj)
CodePudding user response:
Alternative way to implement simular feature is to use __init_subclass__
method
class Faculty:
dictionary= {}
def __init_subclass__ (cls, **kwargs):
Faculty.dictionary[cls.__name__] = cls
class New(Faculty):
pass
print(Faculty.dictionary)
Output: {'New': (class '__main__.New')}