我发现自己反复键入此代码以在代码中进行调试。有没有办法为此创建函数?
var abc = 1
console.log("abc = "+abc);
var xyz = 2;
console.log("xyz = "+xyz);
Run Code Online (Sandbox Code Playgroud)
我想创建一个像这样的函数:
logVar = function(input){
console.log(input.name()+" = "+input);
}
Run Code Online (Sandbox Code Playgroud)
有没有办法做到这一点?
logVar(abc)应该返回“ abc = 1”
logVar(xyz)应该返回“ xyz = 2”
我首先创建一个字典作为原始字符串,然后使用 b = eval(raw string) 定义 b。当我将原始字符串剪切并粘贴到 python 控制台时,效果很好,但是当我运行程序时,出现错误“TypeError:字符串索引必须是整数”
最小代码示例:
def drawBoardData2():
cells = ''
cells+=("r\'{")
for r in range(0,2):
c = 2
cc = "light"
piece = "b"
player = "A"
if r != 1:
comma = ","
else:
comma = ""
cells += ("\"{r}.{c}\":[1,1,\"{color}\",\"{pi}\",\"{pl}\",\"none\"]{cm} ".format(r=r, c=c, color=cc, pi=piece, pl=player, cm=comma))
cells += ("}\'")
#print("}\'")
print(cells)
b = eval(cells)
print(b)
print(type(b)) # result: <class 'str'> instead of <class 'dict'>
b["1.2"] # result: "TypeError: string indices must be integers"
drawBoardData2() …Run Code Online (Sandbox Code Playgroud)