-1

I was trying to make variables inside a loop. i.e. I pass a pattern of variables, and the pattern of their values and the variables are accordingly created and stored in a text file.

But, I tried something off topic and did this:

a = lambda a: a
for i in ["a", "b"]:
    b = eval(i)(a)
    print(i)

the output was:

a
b

Can anyone please explain what has happened here?

Edit: I have analysed its answer. I will paste it below. Please verify if it is correct. Thank you!

Lets first break the problem in parts.

def a(n):
    return n
b = eval("a")(a)
print("a")
b = eval("b")(a)
print("b")

We can clearly see that the output is due to the two print statements.

print("a")
print("b")

Thus the rest of the statements play no part in the output.

def a(n):
    return n
b = eval("a")(a)
b = eval("b")(a)

These statements can simply be put across like this:

def a(n):
    return n
b = a(a)
b = b(a)

The statement

b = a(a)

makes the same effect as

def b(n):
    return n

Thus the entire code can be put across like this:

def a(n):
    return n
def b(n):
    return n
print("a")
print("b")

Thus there is no ambiguity in this question now.

Ishaan Kapoor
  • 334
  • 2
  • 6
  • Your looping through ["a", "b"] (a and b) and just printing it. Do print B for different results – Evorage Oct 12 '20 at 20:21
  • 2
    “I was trying to make variables inside a loop.” — **Don’t**. Heed the advice from https://stackoverflow.com/a/64322465/1968. – Konrad Rudolph Oct 12 '20 at 20:25
  • I second the nomination: making individual variables with such processing is unnecessary and dangerous. Work through tutorials on more data structures: lists, dicts, classes, etc. so you have the tools to fit your problems. – Prune Oct 12 '20 at 20:31
  • @Prune Thank you! Btw. I just stumbled across this while working on a project and figured out what had happened the next day. Btw. Thank you very much! – Ishaan Kapoor Nov 06 '20 at 13:44

3 Answers3

0

Here's how you can deconstruct your loop to understand for yourself, and please don't do that as pointed out in the comment.

a = lambda a: a

# First iteration
i = "a"
b = eval(i)(a)
print(i) # a

# Second iteration
i = "b"
b = eval(i)(a) # eval("b") is now <function __main__.<lambda>(a)>
print(i) # b
DaveIdito
  • 1,546
  • 14
  • 31
0

You are printing the variable i (which takes the values "a" and "b" since you loop over ["a", "b"]). If you want to see which values the variable b takes, print b instead:

a = lambda a: a
for i in ["a", "b"]:
   b = eval(i)(a)
   print(b)
beachy
  • 101
  • 4
0

Lets first break the problem in parts.

def a(n):
    return n
b = eval("a")(a)
print("a")
b = eval("b")(a)
print("b")

We can clearly see that the output is due to the two print statements.

print("a")
print("b")

Thus the rest of the statements play no part in the output.

def a(n):
    return n
b = eval("a")(a)
b = eval("b")(a)

These statements can simply be put across like this:

def a(n):
    return n
b = a(a)
b = b(a)

The statement

b = a(a)

makes the same effect as

def b(n):
    return n

Thus the entire code can be put across like this:

def a(n):
    return n
def b(n):
    return n
print("a")
print("b")

Thus there is no ambiguity in this question now.

Ishaan Kapoor
  • 334
  • 2
  • 6