0

Getting undefined error with the below program

class SignalHandler(object):  # pylint:  disable=too-few-public-methods
"""
Handles various user generated signals
"""

    def __init__(self,
             sigint_signalhandler=SignalHandler.signal_handler,
             sigquit_signalhandler=SignalHandler.signal_handler,
             sighup_signalhandler=SignalHandler.signal_handler):

        signal.signal(signal.SIGINT, sigint_signalhandler)
        signal.signal(signal.SIGQUIT, sigquit_signalhandler)
        signal.signal(signal.SIGHUP, sighup_signalhandler)

    @staticmethod
    def signal_handler(signalnum):
        print ('Ignoring signal : ', signalnum)

This is what the error looks like

import signalhandler
File "/usr/local/sandvine/scripts/upgrade-assistance/signalhandler.py", line 10, in <module>
class SignalHandler(object):  # pylint:  disable=too-few-public-methods
File "/usr/local/sandvine/scripts/upgrade-assistance/signalhandler.py", line 22, in SignalHandler
sigint_signalhandler=SignalHandler.signal_handler,
NameError: name 'SignalHandler' is not defined

So eventually i want to pass some custom methods, if not i will use signal_handler method provided by SignalHandler class.

Ronin Goda
  • 234
  • 5
  • 13
  • Possible duplicate of [assigning class variable as default value to class method argument](https://stackoverflow.com/questions/15189245/assigning-class-variable-as-default-value-to-class-method-argument) – Taku Apr 23 '18 at 09:59

1 Answers1

1

Define signal_handler above the class as a plain method, not inside it as a static method. You can't refer to a class before it's been defined, which is what your __init__ is trying to do.

Alternatively, use self.signal_handler in the init body instead of SignalHandler.signal_handler:

class Foo(object):
    def __init__(self, bar_printer=None):
        self.bar_printer = bar_printer if bar_printer else self._default_bar_printer

    @staticmethod
    def _default_bar_printer():
        print("bar")


f = Foo()
f.bar_printer()  # Prints "bar"

def better_bar_printer():
    print("The best bar!")

f2 = Foo(better_bar_printer)
f2.bar_printer()  # Prints "The best bar!"
Tom Dalton
  • 6,122
  • 24
  • 35
  • And after class creation the function can be added to the class using `SignalHandler.signal_handler = staticmethod(signal_handler)`. – a_guest Apr 23 '18 at 09:57
  • Ok now it's clear. By the way you could use simplified version `self.bar_printer = bar_printer or self._default_bar_printer` in `__init__`. – a_guest Apr 23 '18 at 10:23
  • I have a personal (probably unfair/irrational) dislike of using `or` for that purpose. Possibly due to childhood exposure to C constructs that use `or` alongside functions with side-effects :-) – Tom Dalton Apr 23 '18 at 10:27
  • Okay, but actually `or` in Python does always the same thing, no matter what the context is (i.e. it is the same when you use it for that purpose and when you use it within `if` clauses for example): `or` yields the left-hand side if it evaluates to `True` and the right-hand side otherwise. So it is exactly the same thing as you coded it explicitly. – a_guest Apr 23 '18 at 10:40