0

I'm new with Python and I've encountered this error this afternoon. I tried to fix this by adding global before the previous variable but I continue to get this error:

Traceback (most recent call last):
  File "send.py", line 76, in <module>
main(sys.argv[1:])
  File "send.py", line 34, in main
send()
  File "send.py", line 29, in send
    if data != previous:

A sample of the code I did:

import socket
import sys
import getopt
import time
import threading

sys.path.insert(0, '/usr/lib/python2.7/bridge/') 

from bridgeclient import BridgeClient as bridgeclient

def main(argv):
    global bridge
    global previous

    try:                    
        # Create a UDP socket.
        sock = socket.socket(socket.AF_INET, socket.SOCK_DGRAM)
        server_address = ('192.168.1.100', 9050)

        bridge = bridgeclient()
        previous = ""

        # Send data  
        def send():         
            data = bridge.get("data")   

            if data != previous:                    
                sent = sock.sendto(data, server_address)                                
                previous = data
                threading.Timer(0.2, send).start()

        send()

    finally:
        sock.close()

if __name__ == "__main__":
    main(sys.argv[1:])
7
  • You should post the traceback so we can see the details of the error. Also, the BridgeClient import is not likely related but makes it so we can't test your code. Post something smaller that demonstrates the issue. You could copy this file and delete half of it including the socket calls. Commented Jan 18, 2017 at 22:12
  • don't use global and don't use nested functions. Commented Jan 18, 2017 at 22:12
  • @tdelaney I've updated my post Commented Jan 18, 2017 at 22:15
  • @Daniel I use nested function to be able to control the Timer. Maybe I'm wrong but I read somewhere that this technique is better than a sleep() function. I'm not much aware of good practice in Python (I'm new!) but if you say that nested function is a bad practice, I will try avoid this! Commented Jan 18, 2017 at 22:19
  • @Daniel that's a rather broad statement. There is nothing wrong with nested functions - its more properly called a "closure" - especially when scheduling future work like a timer. This particular implementation needs some fixes but its not a bad strategy in general. Commented Jan 18, 2017 at 22:27

1 Answer 1

2

You have nested scopes here:

def main(argv):
    ...
    global previous
    ...
    def send():
        ...
        if data != previous:

Declaring global in main function does not apply to the local in send function.

You could move the global declaration for the previous into the start of the send method. You can remove the global declaration for bridge completely.

Better yet, refactor your code not to use nested scopes and global variables!

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

Comments

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.