Nikhil Gupta
Nikhil Gupta

Reputation: 561

re-using the variable in a function in a class in python

I will try to explain the problem I am facing with a small piece of code:

class MyHandler(PatternMatchingEventHandler):
    patterns = ["*.csv","*.processing", "*.transforming","*.loading"]

    def process(self, event):
        eventFileName = event.src_path
        eventType = event.event_type
        if eventType == 'moved':
            eventFileName = event.dest_path
        fileNameWithPath, fileExtension = os.path.splitext(eventFileName)

        if fileExtension == '.processing': 
            # Here some function is called to do something, and then appends ".loading" to the file name
            testVariable = 75.3
        if fileExtension == '.loading':
            print testVariable
    def on_moved(self, event):
        self.process(event)

    def on_created(self, event):
        self.process(event)

if __name__ == '__main__':
    observer = Observer()
    observer.schedule(MyHandler(), path='.')
    observer.start()

    try:
        while True:
            time.sleep(1)
    except KeyboardInterrupt:
        observer.stop()

    observer.join()

When I try to do the above I am getting this error: global name 'testVariable' is not defined which kinda makes sense but how do I make the above work? I tried to define "testVariable" globally and initiated to 0 and tried using it the way I showed in above code but it did not work as well. I also tried initiating testVariable as testVariable=0 inside the class (right after "patterns = ....." line but I got this error: local variable "testVariable" referenced before assignment pointing towards print testVariable. So that didnot work as well.

Upvotes: 0

Views: 55

Answers (3)

jDo
jDo

Reputation: 4010

"(...) how do I make the above work?"

By defining testVariable outside your conditional statements. E.g. here:

def process(self, event):
    eventFileName = event.src_path
    testVariable = 0
    ...

This will make it available within the process function. If you want it to be available throughout the class, you can define it here:

class MyHandler(PatternMatchingEventHandler):
    patterns = ["*.csv","*.processing", "*.transforming","*.loading"]
    testVariable = 0

But then you have to access it via the self object within functions like so:

def process(self, event):
    ...
    if fileExtension == '.processing': 
        # Here some function is called to do something, and then appends ".loading" to the file name
        self.testVariable = 75.3

Upvotes: 2

Alex Hall
Alex Hall

Reputation: 36033

def process(self, event):
    def extension():
        eventFileName = event.src_path
        eventType = event.event_type
        if eventType == 'moved':
            eventFileName = event.dest_path
        return os.path.splitext(eventFileName)[1]

    if extension() == '.processing': 
        ...
    if extension() == '.loading':
        ...

Upvotes: 0

Jacob Panikulam
Jacob Panikulam

Reputation: 1218

testVariable only exists if you have the extension ".processing". If it's ".loading", the program tries to print a variable that hasn't been made to exist.

If statements do not create a garbage collecting scope in Python, so you don't have to "declare" it outside, so long as somewhere in your if-tree, tesVariable gets a value.

Upvotes: 0

Related Questions