0

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)
ouroboros1
  • 9,113
  • 3
  • 7
  • 26

3 Answers3

0

Try this:

def addFaculty(self, f_obj): 
    return isinstance(f_obj, Faculty)

or:

def addFaculty(self, f_obj):
    return type(f_obj) is Faculty 
  • Just making it worse. The second form doesn't account for subclasses. – tdelaney Oct 22 '22 at 23:18
  • ahh yeah forgot about the append part, but really curious about the subclass thing tho. – Fares_Hassen Oct 22 '22 at 23:24
  • tho i think it will depend on the use case o the checking if he wants that class or a sub or just that class – Fares_Hassen Oct 22 '22 at 23:25
  • Suppose someone creates `class TemporaryFaculty(Faculty)` - the test would fail. But that's fundamental to OOP polymorphism. A subclass should be able to take the place of its parent. – tdelaney Oct 22 '22 at 23:45
  • @tdelaney then what if the desired output is to check if and only if the object is an instance to that class nd not one of it's children? – Fares_Hassen Oct 22 '22 at 23:50
  • That would be extremely uncommon - such an exceptional requirement would need to be noted in the question. – tdelaney Oct 23 '22 at 01:13
0

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)
xillmera
  • 22
  • 3
  • No, on multiple levels. You still have the syntax error and the signature is `isinstance(obj, class_or_tuple, /)` – tdelaney Oct 22 '22 at 23:17
  • Of cource, becouse i copied your (@tdelaney) code fully. Syntax error with using `isinstance` func. Signature of which is `def isinstance (obj, class_or_tuple, /) -> bool:`. Annotation is important. If constraction mention to use bool value to branch code, but u place to values next to each other ` bool`. This not matching any regular expression that use python interpriter in this context – xillmera Oct 22 '22 at 23:30
  • But why test against `self.__class__` when you want to test against `Faculty`? It would only work if this is another method of the `Faculty` class and would break with a subclass of `Faculty`. Just use `Faculty`. – tdelaney Oct 22 '22 at 23:44
  • @tdelaney, you right. But if you want to store all subclasses there is another way. It is using `__init_subclass__` method. – xillmera Oct 23 '22 at 00:13
  • That is going even further afield. Why would you want custom class cresation to I guess add some facility to do what `isinstance(obj, class)` already does? You say _Class name not acceptable to use from method scope_ and that just isn't true. – tdelaney Oct 23 '22 at 01:12
-1

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')}

xillmera
  • 22
  • 3