1

a.py:

def all():
    n = 2
    while True:
        yield n
        n = n + 1

y = all()
number = next(y)
print(number)
y = filter(lambda x: x % number != 0, y)
number = next(y)
print(number)
y = filter(lambda x: x%3 != 0, y)
print(next(y))
y = filter(lambda x: x%5 != 0, y)
print(next(y))
y = filter(lambda x: x%7 != 0, y)

b.py:

def all():
    n = 2
    while True:
        yield n
        n = n + 1

y = all()
number = next(y)
print(number)
y = filter(lambda x: x % number != 0, y)
num = next(y)
print(num)
y = filter(lambda x: x%3 != 0, y)
print(next(y))
y = filter(lambda x: x%5 != 0, y)
print(next(y))
y = filter(lambda x: x%7 != 0, y)

I am trying write a script that print primes.

the output of a.py are: 2 3 4 7. the output of b.py are: 2 3 5 7. The variables, num and number, are never used after y = filter(lambda x: x%3 != 0, y) why the output are different after y = filter(lambda x: x%3 != 0, y)?

user1602200
  • 11
  • 1
  • 2
  • @pingu I thought the same thing when I first looked but the line `number = next(y)` and `num = next(y)` are the difference in these, and because `number` is used in the lambda, when its changed in `a.py` it affects what the lambda produces – Chris Doyle Jan 21 '23 at 11:30
  • I think you have accidentally pasted the outputs the wrong way around. `a.py` outputs `2 3 4 7` whereas `b.py` outputs `2 3 5 7`. – Lecdi Jan 21 '23 at 11:37
  • @Lecdi thanks for your reminding~ I just fix it. – user1602200 Jan 21 '23 at 12:31

2 Answers2

2

You are using different variables in a and b. The different is because you set a lambda that will use the value of the variable number as part of the calculation.

In a.py you set the number before the filter, then you change the value of number after you create the filter, this changes the value inside the filter.

In b.py you set the number before the filter then you create a new var called num after the filter, since this is a different var it doesn't alter the outcome of the filter.

We can illustrate this with a simple example.

print("example A")
number = 2
my_lambda = lambda x: x + number
print(my_lambda(2))
number = 5
print(my_lambda(20))
print(my_lambda(200))

print("example B")
number = 2
my_lambda = lambda x: x + number
print(my_lambda(2))
num = 5
print(my_lambda(20))
print(my_lambda(200))

OUTPUT

example A
4
25
205
example B
4
22
202
Udesh
  • 2,415
  • 2
  • 22
  • 32
Chris Doyle
  • 10,703
  • 2
  • 23
  • 42
0
def all():
    n = 2
    while True:
        yield n
        n = n + 1

y = all()
number = next(y)

y = filter(lambda x: x % number != 0, y)

# After the filter now y is similar to

def fun():
    for x in all():
        if x % number != 0:
            yield x

y = filter(lambda x: x%3 != 0, y)

# After the filter now y is similar to

def fun():
    for x in all():
        if x % number != 0 and x % 3 != 0:
            yield x

y = filter(lambda x: x%5 != 0, y)

# After the filter now y is similar to

def fun():
    for x in all():
        if x % number != 0 and x % 3 != 0 and x % 5 != 0 :
            yield x

filter(condition, sequence) method will return a generator using which you can generate new value But notice since you have used number as a variable it exits in the nested condition.

In future if you change the value of number it will generate different sequence because number exists in the nested condition.

Hope you got me.

Udesh
  • 2,415
  • 2
  • 22
  • 32