基于整数创建多个变量 [英] Creating multiple variables based on an integer

查看:37
本文介绍了基于整数创建多个变量的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我想根据我定义的数字创建多个变量.
目前,我有一个客户端和一个服务器在运行,每次客户端加入时,我都希望服务器创建一个变量.每个用户都有一个编号:

I want to create multiple variables based on a number that I have defined.
Currently, I have a client and a server running and every time a client joins, I want the server to create a variable. Each user is assigned a number:

list_of_addr = []
user_num = 0
recv_verf, addr = server_socket.recvfrom(2048)
if(recv_verf == 'connect'):
    recv_user, addr = server_socket.recvfrom(2048)
    list_of_addr.append('User:' + recv_user + ' # ' + str(user_num) + '\n')
    user_num = user_num + 1
    print 'User:' + recv_user + ' # ' + str(user_num) + ' connected'

我想根据这个数字在这里创建一个变量.类似于 user_(user_num) = 0.
我不知道如何解释这一点.抱歉,这是模糊的.

I want a variable to be created here based on this number. Similar to user_(user_num) = 0.
I don't know how else to explain this. Sorry is this is vague.

推荐答案

不要这样做.如果您想拥有一组由名称指向的动态项目,这就是 dict 的用途.如果您只想要按特定顺序排列的项目列表,这就是 list 的用途.

Don't do this. If you want to have a dynamic group of items pointed to by names, that's what a dict is for. If you just want an list of items in a particular order, that's what a list is for.

变量名应该与您(程序员)在代码中实际编写的内容保持一致.另一条路是疯狂.

Variable names should be kept to what you, the programmer, actually write in your code. Down the other path lies madness.

这篇关于基于整数创建多个变量的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆