Assign Constructor argument with static method pointer in python? Code Answer

Hello Developer, Hope you guys are doing great. Today at Tutorial Guruji Official website, we are sharing the answer of Assign Constructor argument with static method pointer in python? without wasting too much if your time.

The question is published on by Tutorial Guruji team.

Getting undefined error with the below program

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

    def __init__(self,

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

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

This is what the error looks like

import signalhandler
File "/usr/local/sandvine/scripts/upgrade-assistance/", line 10, in <module>
class SignalHandler(object):  # pylint:  disable=too-few-public-methods
File "/usr/local/sandvine/scripts/upgrade-assistance/", line 22, in SignalHandler
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.


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

    def _default_bar_printer():

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!"
We are here to answer your question about Assign Constructor argument with static method pointer in python? - If you find the proper solution, please don't forgot to share this with your team members.

Related Posts

Tutorial Guruji