Follow

Keep Up to Date with the Most Important News

By pressing the Subscribe button, you confirm that you have read and are agreeing to our Privacy Policy and Terms of Use
Contact

What does having o_ (o underscore) do in dir?

I was reviewing the code from this donut.py code that produces an ASCII spinning taurus like the donut.c code.

I was wondering what having these 2 snippets in the code does?

'o_' in dir()
# and
(o_ := True)

Just looking at the code they seem useless but when I remove them the code doesn’t run. I know what the walrus op does and from what I’m guessing it just ensures the loop runs infinitely. And I tried replacing the first line with False and the second with True

MEDevel.com: Open-source for Healthcare and Education

Collecting and validating open-source software for healthcare, education, enterprise, development, medical imaging, medical records, and digital pathology.

Visit Medevel

Here’s the code for reference:

while ('o_' in dir()) or (A := (0)
               ) or (B := 0) or print((                           
            "\x1b[2J")) or not ((sin := ((                        
         __import__('math'))).sin)) or not (                     
      cos := __import__('math').cos) or (o_ := (                   
     True)): [pr() for b in [[(func(), b) for ((z                 
    )) in [[0 for _ in range(1760)]] for b in [[ (                
   "\n") if ii % 80 == 79 else " " for ii in range(               
  1760)]] for o, D, N in [(o, D, N) for j in range((              
 0), 628, 7) for i in range(0, 628, 2) for (c, d, e,(             
f), g, l, m, n) in [(sin(     i / 100), cos(j / 100),(             
sin(A)), sin(j / 100),          cos(A), cos(i / 100) ,            
cos(B), sin(B))] for              (h) in [d + 2] for (            
D, t) in [ (1 / ( c               * h * e + f * g + (5
)), c * h * g - f *                e)] for (x, y) in [            
(int( 40 + 30 * D * (              l * h * m - t * n)),            
int( 12 + 15 * D * ( l           * h * n + t * (m))))]            
 for (o, N) in [(x + 80 *     y,int(8 * (( f * e -  c              
  * d * g) * m - c * d * e - f * g - l * d * n)))] if             
  0 < x < 80 and 22 > y > 0] if D > z[o] for func in              
   [lambda: (z.pop((o))), lambda: z.insert((o), (D)                
    ),(lambda: (b.pop( o))), lambda: b.insert((o),                 
     ".,-~:;=!*#$@"[ N if N > 0 else 0])]][ 0][1]                 
       ] for pr in [lambda: print("\x1b[H"),                    
         lambda: print("".join(b))] if (A :=
            A + 0.02) and ( B := B + 0.02)]                        
               #..--~~EvanZhouDev:~~--.#                           
                    #...,2023,...#

>Solution :

dir() returns a list of all the variable names in the current scope, so in this case it returns all the global variables.

So 'o_' in dir() is true when there’s a global variable named o_.

This will become true after o_ := (True) is executed, since that creates the variable with that name.

This is being used as a flag variable. Testing a variable value would require initializing it before the loop.

The walrus operator has to be used because everything is being done using short-circuiting operations in the while condition and a single body statement consisting of a list comprehension. This is needed to get around Python’s indentation rules, so the code can have the donut shape.

Add a comment

Leave a Reply

Keep Up to Date with the Most Important News

By pressing the Subscribe button, you confirm that you have read and are agreeing to our Privacy Policy and Terms of Use

Discover more from Dev solutions

Subscribe now to keep reading and get access to the full archive.

Continue reading