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.

1

1 Answer 1

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!"
Sign up to request clarification or add additional context in comments.

4 Comments

And after class creation the function can be added to the class using SignalHandler.signal_handler = staticmethod(signal_handler).
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__.
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 :-)
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.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.