2

I am using a python socket and one of the API of the socket is

 (clientsocket, address) = serversocket.accept()

My magic number way is

 connected_sock = serversocket.accept()[0]

However, I am interested only in client socket and not address. I can do so that there is just the client socket value and not address. What is the appropriate way in Python to do so that I can avoid magic numbers in my code?

1 Answers1

3

You can use _ as a throwaway variable:

clientsocket, _ = serversocket.accept()
enzo
  • 9,861
  • 3
  • 15
  • 38
  • 2
    Maybe worth noting that `_` became a bit more than just a convention. Static analysis tools will not complain about `_` being defined but not used, nor about it being defined multiple times. – DeepSpace Oct 26 '21 at 23:04
  • Your choices are `_` to indicate "I don't care", or `_socket` to indicate "I don't care for now, but I may need this in the future, so I want to remember that it's a socket. In both cases, static analysis tools will not complain. – Frank Yellin Oct 26 '21 at 23:08