Last active
September 19, 2021 23:01
-
-
Save LightAndLight/3500e6cb28af865095c8f2bab46cb62f to your computer and use it in GitHub Desktop.
Python `wat`s
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
######### | |
# What happens when you run this? | |
print(1 + 0001) | |
# Does this code run? If not- why? If so- what is the value of `result`? | |
class Foo: | |
foo = 0 | |
myFoo = Foo() | |
result = [myFoo for myFoo.foo in [1,2,3]] | |
# What about this? | |
class Foo: | |
foo = 0 | |
myFoo = Foo() | |
result = [myFoo for (myFoo.foo if True else myFoo.foo) in [1,2,3]] | |
######## | |
def f(x): | |
return f(x[0]) | |
# What does f(1) do? | |
# What does f([1,2,3]) do? | |
# What does f("abc") do? | |
######## | |
# 1. What does this code do? | |
def b(): | |
a = 2 | |
del a | |
return a | |
print(b()) | |
# 2. What does this code do? | |
def b(): | |
def c(): | |
nonlocal a | |
a = 3 | |
c() | |
return a | |
print(b()) | |
# 3. Now, what does this code do? | |
def b(): | |
a = 2 | |
del a | |
def c(): | |
nonlocal a | |
a = 3 | |
c() | |
return a | |
print(b()) | |
######## | |
# FLEXIBILE(TM) | |
# DYNAMIC(TM) | |
# AGILE(TM) | |
def log_this_class(cls): | |
def also_kill_kitten(f): | |
def inner(*args, **kwargs): | |
print("killed a kitten") | |
return f(*args, **kwargs) | |
return inner | |
setattr(cls, "__getattribute__", also_kill_kitten(cls.__getattribute__)) | |
return cls | |
@log_this_class | |
class Foo: | |
foo = 2 | |
print(Foo().foo) | |
######## | |
def test(): | |
return lambda a=(yield 2): a | |
x = test() | |
print(x(1)) | |
######## | |
x = 1 | |
for x in [2]: | |
pass | |
print(x) |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
wat!