Def() output <gen. obj>

How would you obtain the x values without modifying the code’s structure or making only a minor adjustment?

def f():
    return (x for x in range(-100, 100)
            if any(math.sqrt(x**2 + x - 12)) ==
            y for y in range(-100, 100))

print(f())

You probably should first write some sensible code, it does not make any sense at all. The list comprehension is broken and your use of any is faulty.

What are you even trying to do with the comprehension?

You should

  1. write out your thoughts in natural language in a clear way
  2. write simple code
  3. write clear code

P.S. I hope you’re not throwing here AI generated code.

I think AI will outperform most programmers.
However, I don’t want to be in that categ.

Above all, my primary objective has been to unlock Pythonic languages.

  1. any cannot iterate over float
  2. you can’t sqrt all values you pass in
  3. the tuple comprehension in def f() is wrong (or I guess generator expression)

You get a generator object, because the code in the expression (I guess generator expression) is not yet evaluated. This is why you haven’t gotten any exceptions yet.

Write your print(f()) as print(list(f()) and you’ll see you are having errors in your expression.

Again, it is unclear what you are trying to do: the code for f() is incorrect, obtuse and hard to read.

Your set comprehension has at least the correct syntax compared to the generator expression.

FWIW, writing code is a dialog with future readers. It is your job to make it clear and easy to understand.

Anyway, right now AI creates crap code. Maybe some day in the future it generates usable code, but not right now. Don’t use it as is, especially if you don’t fully understand what the generated code does.

1 Like