variable-variables

How do I import variable packages in Python like using variable variables ($$) in PHP?

 ̄綄美尐妖づ 提交于 2019-11-26 10:27:02
问题 I want to import some package depending on which value the user chooses. The default is file1.py : from files import file1 If user chooses file2 , it should be : from files import file2 In PHP, I can do this using variable variables: $file_name = \'file1\'; include($$file_name); $file_name = \'file2\'; include($$file_name); How can I do this in Python? 回答1: Python doesn't have a feature that's directly equivalent to PHP's "variable variables". To get a "variable variable"'s value (or the

“Variable” variables in Javascript?

Deadly 提交于 2019-11-25 21:34:52
问题 I know it\'s possible in PHP to have \"variable\" variables. For example $x = \"variable\"; $$x = \"hello, world!\"; echo $variable; // displays \"hello, world!\" Is it possible to refer to a variable by its name as a string in javascript? How would it be done? 回答1: There is no single solution for this (well, there is eval , but lets not seriously consider that). It is possible to access some global variables dynamically via window , but that doesn't work for variables local to a function.

How do I create a variable number of variables?

隐身守侯 提交于 2019-11-25 21:30:05
问题 How do I accomplish variable variables in Python? Here is an elaborative manual entry, for instance: Variable variables I have heard this is a bad idea in general though, and it is a security hole in Python. Is that true? 回答1: You can use dictionaries to accomplish this. Dictionaries are stores of keys and values. >>> dct = {'x': 1, 'y': 2, 'z': 3} >>> dct {'y': 2, 'x': 1, 'z': 3} >>> dct["y"] 2 You can use variable key names to achieve the effect of variable variables without the security