Inline Python Matrix Binary!
Today, I challenged myself to remake a Randomized Binary print function, inspired by The Matrix!
But wait! There's more!
I did not just remake it, but I remade it in one line of Python code!
Now, I want to clarify something...
In my opinion , one liners in Python should not include any commas or semicolons, as semicolons are quite obviously cheating in Python one liners, and commas, at least, in my experience, achieve a very similar effect.
So, instead, I have to find a way to use multiple functions in one line, and to do that, I have a few different methods!
- First of all, I default lambda attributes to functions, whether or not they return anything, for example, this:
print(lambda x = print("Hello World!"): "Bananas")())
Will print "Hello World!" and then "Bananas" (I know, it is weird)
- My second method, which I use when I need to run a function defined in another lambda attribute, is to embed lambdas to use the parent lambda's attributes/functions, like this (I also use an inline import for this, which is very helpful):
print((lambda x = __import__("random").uniform: lambda y = x(0, 1))())
This (^) will import random.uniform() as x and then print a random float between 0 and 1 using x(0, 1)
- Notice the print at the beginning of the previous code and how the two parentheses near the end are open, this will run the lambda when it is defined without having to set it to a variable then run it. After running, the return value of the lambda is then printed thanks to the print function enclosing the lambda! Which is very similar to this functionality:
def function(x): return x print(function(3))
Alright, I think that's about it!
Have fun watching the green binary flowing across your screen, take a look at the code in the second file (multiline.py) to see the original version!