创建一个变量为键的字典

时间:2018-04-06 17:11:25

标签: python-3.x

以下是KeyErrors:

let app = require('express')();
let http = require('http').Server(app);
let io = require('socket.io')('http');

io.on('connection', (socket) =>{
    //Log when a user connects
    console.log('user connected');

    //Log when a client disconnects
    socket.on('disconnect', function(){
        console.log('user disconnected');
    });

    //When we receive a message event from our client, print out the 
    //contents of that message and echo it back to our clients using emit()

    socket.on('message', (message)=>{
        console.log('message received: ' + message);
        io.emit('message', {type:'new-message', text: message});
    });
});

//initialize server on port 5000
http.listen(5000, () =>{
    console.log('started on port 500');
});

代替: {'a':'Some Text','d':04-06-2018}

哪里出错?

1 个答案:

答案 0 :(得分:0)

可以使用漂亮的字典理解。

要获取所有全局范围变量,您只需执行以下操作:

global_var = {key: globals()[key] for i in args for key in globals().keys() if i == globals()[key]}

要获取所有本地作用域变量,您可以直接使用以下内容:

local_var = {key: locals()[key] for i in args for key in locals().keys() if i == locals()[key]}
  

如果您一起使用它们,请确保将它们添加到单个dict中,以免您修改正在迭代的集合

如果您在python refer to this

中了解有关范围的更多信息