如果我在 python 文件中有一个包含一些代码的字符串。我想在字符串中运行代码,例如:
program = "for i in range(100):\n\tprint(i)"
eval(program)
Run Code Online (Sandbox Code Playgroud)
但是,我很确定 eval 仅适用于数学运算,因此会引发语法错误:
Traceback (most recent call last):
File "main.py", line 2, in <module>
eval(program)
File "<string>", line 1
for i in range(100):
^
SyntaxError: invalid syntax
Run Code Online (Sandbox Code Playgroud)
python中有没有办法运行包含在字符串中的代码?
namespace WindowsFormsApplication3
{
public partial class Form1 : Form
{
public Form1()
{
InitializeComponent();
}
int A = 0;
private void timer1_Tick(object sender, EventArgs e)
{
A++;
if (A == 30)
{
A--;
}
textBox1.Text = A.ToString();
}
}
}
Run Code Online (Sandbox Code Playgroud) 我只是想知道是否可以在 useEffect 挂钩内渲染组件。我有两个组件,假设 A 和 B。在组件 A 中,我的 useEffect 挂钩如下。在测试钩子内的某些条件时,我想渲染组件 B。
export default function A() {
const [location, setlocation] = usestate(null);
useEffect(() => {
if (condition) {
<B />; // it does not render anything though
} else {
//code
}
}, [location]);
}
export default function B() {
const test = () => {
console.log("testing");
};
return (
<View style={styles.container}>
<Button title={"Click Me"} onPress={test} />
</View>
);
}
Run Code Online (Sandbox Code Playgroud) 尝试编译此代码.为什么ctor工作3次?在输出之前添加断点并调试它.
private static void Main(string[] args)
{
Console.WriteLine(new A().a);
}
class A
{
public int a;
public A()
{
Console.WriteLine(GetHashCode());
}
}
Run Code Online (Sandbox Code Playgroud)
输出:

我有以下清单:
lst = ['123', '456', [['123', '456']], ['123', '456']]
Run Code Online (Sandbox Code Playgroud)
我想用每个列表的元素也以相反的顺序反转列表。输出应如下所示:
lst = [['654', '321'], [['654', '321']], '654', '321']
Run Code Online (Sandbox Code Playgroud) c# ×2
python ×2
class ×1
constructor ×1
expo ×1
javascript ×1
list ×1
nested ×1
react-hooks ×1
react-native ×1
reactjs ×1
reverse ×1
winforms ×1