Creating multiple variables based on an integer

后端 未结 2 1248
Happy的楠姐
Happy的楠姐 2021-01-27 10:55

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 t

相关标签:
2条回答
  • 2021-01-27 11:38

    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.

    0 讨论(0)
  • 2021-01-27 11:51

    A dict would be an appropriate solution for this :

    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(dict(user=recv_user, number=user_num))
        user_num = user_num + 1
        print 'User: {0} # {1} connected'.format(list_of_addr[-1]['user'], list_of_addr[-1]['number']
    
    0 讨论(0)
提交回复
热议问题