0

I'm a newby to python so please excuse the nomenclature stolen from other languages. I have a class containing a "static" attribute (a list) and a static method. I want to initialise the param to contain just a reference to the method:

class LogFilter(object):

    @staticmethod
    def _process_friendly_time(params):
        # process params
        pass

    param_processors = [
        LogFilter._process_friendly_time
    ]

# Later
for processor in LogFilter.param_processors:
    processor(params)

This code causes an error

NameError: name 'LogFilter' is not defined`.  

But if I replace LogFilter._process_friendly_time with just _process_friendly_time then I later get an error...

TypeError: 'staticmethod' object is not callable

Is there a syntax that will let me do this, or must I move the static method outside the class?

1 Answer 1

1

You can't refer to LogFilter._process_friendly_time at a point before LogFilter has been defined. At the point where you are currently declaring param_processors, you are midway through the definition of LogFilter: the definition is not yet complete.

You can move the field declaration to after the class.

class LogFilter:
    @staticmethod
    def _process_friendly_time(params):
        # process params
        pass

LogFilter.param_processors = [
    LogFilter._process_friendly_time
]

But consider whether you are actually getting any benefit out of using a static method. People often use them out of habit from other languages, but there is no benefit in this case: a standalone function would make more sense.

Sign up to request clarification or add additional context in comments.

Comments

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.