[英]Return a list of attributes from a list of objects
I'm developing a simple TCP chat application server-client to learn Python and I'm not sure about data structures to use. 我正在开发一个简单的TCP聊天应用程序服务器-客户端以学习Python,但不确定要使用的数据结构。
Server side I've defined client
class and its attributes: socket, username, room and online_status. 服务器端,我已经定义了
client
类及其属性:套接字,用户名,房间和online_status。 Every client is added to clients
list and also, at the same time, its socket is added to connections
list. 每个客户端都添加到
clients
列表,同时,其套接字也添加到connections
列表。 Server runs in an always-true loop listening for incoming messages using the following declaration 服务器使用以下声明在始终为真的循环中运行,以侦听传入的消息
(ready_to_read, _, _) = select.select([server_socket] + list(connections), [], [])
Is there any way to list every socket
of every client
objects and do like 有没有办法列出每个
client
对象的每个socket
并执行
(ready_to_read, _, _) = select.select( list(client.socket), [], [])
Unsure that I have really understood what you want, but assuming that you want to build a list of sockets from the list of client instances, you can use a list comprehension: 不确定我是否真正了解您想要的内容,但是假设您想从客户端实例列表中构建套接字列表,则可以使用列表推导:
(ready_to_read, _, _) = select.select( [client.socket for client in clients], [], [])
(assuming clients
is a list of the client instances) (假设
clients
是客户端实例的列表)
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.