tags:

views:

103

answers:

1

Can we store socket objects in a Python dictionary. I want to create a socket, store socket object, do some stuff and then read from the socket(search from dictionary to get socketobject).

+7  A: 

Yes:

>>> import socket
>>> s = socket.socket()
>>> d = {"key" : s}
>>> d
{'key': <socket._socketobject object at 0x00CEB5A8>}
Vinay Sajip
Can we compare these socket objects for equality like:if sokObj1 == sokObj2: #Do some operation
Raj
Which socket objects do you mean, exactly? Obviously if you put `s` into `d` with key "key", then `d["key"]` will return the exact same object `s` which you put in.
Vinay Sajip