Output src1, src2, src3 value for statement without list

Asked 1 weeks ago, Updated 1 weeks ago, 2 views

Can't we have src output as [A,B,C] instead of [src1, src2, src3] without making a list below?

src1 = "A"
src2 = "B"
src3 = "C"

for i in [1,2,3]: 
    src = (f'src{i}')

And what do you call this?


2022-09-20 11:34

1 Answers

This is called dynamic variables or variable variables. If you look at some languages, like PHP, you can see that this is not impossible, and even in some cases, it seems very easy, simple, and reasonable.

$src1 = "A"
$var = 'src1';
echo $$var; // --> $src1 --> "A"

But most languages see it as a very bad habit.

But why is this a bad habit?
The last link I put up is this.

Although Python can generate dynamic variable names, that doesn't mean you have to do this or you have to do this. Because Python generates data dynamically. Python refers to the object of the code. If there are references to objects, there are objects, of course.

What Python refers to is an object, not a set of characters that you accidentally used to create it. And it's actually all programming languages that aren't machine languages. Just because we use the character when we declare and use variables, we should not attempt to approach the program's reference system directly by creating a wonderful coincidence with any set of character adversaries. It causes errors first, it destroys the logical consistency of the second program, and it ultimately causes the monkey to write Shakespeare.

First of all, the problem of causing errors. This is illustrated by one example. Read the code below. What will be printed? Look forward to something and then run Here.

var foo = "A";
var bar = "B";
var variables = ["foo", "bar‎"];

for (var i = 0; i < variables.length; i++) {

Next, the question of logical consistency. src1, src2, and src3 are all types of src, but they're just different orders, right? Even the name is like that. If not, it is a serious problem.You should've changed your name from the beginning Like jack , KTX, compliance. Originally, these are all expected to consist of more systematically controllable materials in the list src with regularity and consistency that are only ordered and in the same format. You don't have to disappoint that expectation. There's a reason why Python has List. A lot of people still do this amateur stuff in practice (such as info1, info2 and info3 columns on the DB table), but that's not a good idea. You don't have to be such an amateur.

Finally, Infinite monkey cleanup. Your name src1 is, I'm sorry, a very accidental name. Monkeys can make code like this someday if they have a lot of time. But you know, is that programming? Is it the work of necessity and deduction to obtain natural and natural results derived from justice and axioms? It's essentially not. What I'm saying is that if you keep going like this, your code will become something logical and unfortunate that works only when "src1", "src2", and "src3" are in the correct place with a very thin probability. Of course, there are people in the world who leave such pitiful things as they are and leave work with only a meme code. Calling them code monkeys would not be a coincidence, nor would you want to be a code monkey.

In a word... Please write a list here!!!
If you have any further questions, I'll take them.

2022-09-20 11:34

If you have any answers or tips

© 2022 pinfo. All rights reserved.