Skip to main content
simplified class name test
Source Link
Jean-François Fabre
  • 140.7k
  • 24
  • 179
  • 246

I'm not a metaclass/class specialist but here's a method that works in your simple case (not sure it works as-is in a complex/nested class namespace):

To check if the method was overridden, you could try a getattr on the function name, then check the qualified name (class part is enough using string partitionning):

class NetworkAnalyzer(object):
    def __init__(self):
        funcname = "_score_funct"
        d = getattr(self,funcname)
        print(d.__qualname__.partition(".")[0] == self.__class__.__name__ + "." + funcname)

if _score_funct is defined in LS, d.__qualname__ is LS._score_funct, else it's NetworkAnalyzer._score_funct.

That works if the method is implemented at LS class level. Else you could replace by:

d.__qualname__ != "NetworkAnalyzer.partition(".")[0] +!= funcname"NetworkAnalyzer"

Of course if the method is overridden with some code which raises an NotImplementedError, that won't work... This method doesn't inspect methods code (which is hazardous anyway)

I'm not a metaclass/class specialist but here's a method that works:

To check if the method was overridden, you could try a getattr on the function name, then check the qualified name:

class NetworkAnalyzer(object):
    def __init__(self):
        funcname = "_score_funct"
        d = getattr(self,funcname)
        print(d.__qualname__ == self.__class__.__name__ + "." + funcname)

if _score_funct is defined in LS, d.__qualname__ is LS._score_funct, else it's NetworkAnalyzer._score_funct.

That works if the method is implemented at LS class level. Else you could replace by:

d.__qualname__ != "NetworkAnalyzer." + funcname

Of course if the method is overridden with some code which raises an NotImplementedError, that won't work... This method doesn't inspect methods code (which is hazardous anyway)

I'm not a metaclass/class specialist but here's a method that works in your simple case (not sure it works as-is in a complex/nested class namespace):

To check if the method was overridden, you could try a getattr on the function name, then check the qualified name (class part is enough using string partitionning):

class NetworkAnalyzer(object):
    def __init__(self):
        funcname = "_score_funct"
        d = getattr(self,funcname)
        print(d.__qualname__.partition(".")[0] == self.__class__.__name__)

if _score_funct is defined in LS, d.__qualname__ is LS._score_funct, else it's NetworkAnalyzer._score_funct.

That works if the method is implemented at LS class level. Else you could replace by:

d.__qualname__.partition(".")[0] != "NetworkAnalyzer"

Of course if the method is overridden with some code which raises an NotImplementedError, that won't work... This method doesn't inspect methods code (which is hazardous anyway)

added 68 characters in body
Source Link
Jean-François Fabre
  • 140.7k
  • 24
  • 179
  • 246

I'm not a metaclass/class specialist but here's a method that works:

To check if the method was overriddentoverridden, you could try a getattr on the function name, then check the qualified name:

class NetworkAnalyzer(object):
    def __init__(self):
        funcname = "_score_funct"
        d = getattr(self,funcname)
        print(d.__qualname__ == self.__class__.__name__ + "." + funcname)

if _score_funct is defined in LS, d.__qualname__ is LS._score_funct, else it's NetworkAnalyzer._score_funct.

That works if the method is implemented at LS class level. Else you could replace by:

d.__qualname__ != "NetworkAnalyzer." + funcname

Of course if the method is overridden with some code which raises an NotImplementedError, that won't work... This method doesn't inspect methods code (which is hazardous anyway)

I'm not a metaclass/class specialist but here's a method that works:

To check if the method was overriddent, you could try a getattr on the function name, then check the qualified name:

class NetworkAnalyzer(object):
    def __init__(self):
        funcname = "_score_funct"
        d = getattr(self,funcname)
        print(d.__qualname__ == self.__class__.__name__ + "." + funcname)

if _score_funct is defined in LS, d.__qualname__ is LS._score_funct, else it's NetworkAnalyzer._score_funct.

That works if the method is implemented at LS class level. Else you could replace by:

d.__qualname__ != "NetworkAnalyzer." + funcname

Of course if the method is overridden with some code which raises an NotImplementedError, that won't work...

I'm not a metaclass/class specialist but here's a method that works:

To check if the method was overridden, you could try a getattr on the function name, then check the qualified name:

class NetworkAnalyzer(object):
    def __init__(self):
        funcname = "_score_funct"
        d = getattr(self,funcname)
        print(d.__qualname__ == self.__class__.__name__ + "." + funcname)

if _score_funct is defined in LS, d.__qualname__ is LS._score_funct, else it's NetworkAnalyzer._score_funct.

That works if the method is implemented at LS class level. Else you could replace by:

d.__qualname__ != "NetworkAnalyzer." + funcname

Of course if the method is overridden with some code which raises an NotImplementedError, that won't work... This method doesn't inspect methods code (which is hazardous anyway)

Source Link
Jean-François Fabre
  • 140.7k
  • 24
  • 179
  • 246

I'm not a metaclass/class specialist but here's a method that works:

To check if the method was overriddent, you could try a getattr on the function name, then check the qualified name:

class NetworkAnalyzer(object):
    def __init__(self):
        funcname = "_score_funct"
        d = getattr(self,funcname)
        print(d.__qualname__ == self.__class__.__name__ + "." + funcname)

if _score_funct is defined in LS, d.__qualname__ is LS._score_funct, else it's NetworkAnalyzer._score_funct.

That works if the method is implemented at LS class level. Else you could replace by:

d.__qualname__ != "NetworkAnalyzer." + funcname

Of course if the method is overridden with some code which raises an NotImplementedError, that won't work...