Pyline Classic
Jump to navigation
Jump to search
Pyline Classic is an esoteric subset of Python 3.5 where the whole source file has to be a single expression. Unlike Pyline, which has a walrus operator, Pyline Classic does not have an ergonomic variable-assignment expression.
Idioms
| Python | Pyline Classic |
|---|---|
if 1 + 1 == 2:
print("Hello, ")
print("World! ")
|
1 + 1 == 2 and (
print("Hello, "),
print("World! ")
)
(
print("Hello, "),
print("World! "),
) if 1 + 1 == 2 else None
(
print("Hello, ") and 0 or
print("World! ")
) if 1 + 1 == 2 else None
|
if i == 1:
...
elif i == 2:
...
else:
...
|
(
...
) if i == 1 else (
...
) if i == 2 else (
...
)
|
for i in range(10):
print(1)
|
[(
print(i),
) for i in range(10)]
next(
None for i in range(10)
if not (
print(i) and 0 or
True
)
)
|
while cond:
print(1)
|
[(
print(1),
) for _ in iter(lambda: cond, False)]
next(
None for _ in __import__("itertools").count()
if not (
print(1) and 0 or
cond
)
)
|
arr[0] += 1 |
arr.insert(0, arr.pop(0) + 1) arr.__setitem__(0, arr[0] + 1) |
obj.var = 5 |
setattr(obj, "var", 5) |
raise err |
(_ for _ in "_").throw(err) |
Examples
Hello World
print("hello world")
Quine
(lambda s:print(f'{s}({chr(34)}{s}{chr(34)})'))("(lambda s:print(f'{s}({chr(34)}{s}{chr(34)})'))")
A shorter one:
print((lambda s:s%s)('print((lambda s:s%%s)(%r))'))
Truth machine
[print("1", end="") for _ in __import__("itertools").count()] if input(":") == "1" else print("0")
A version that doesn't use __import__:
[print(1,end="")for _ in iter(lambda:0,1)]if input(":")=="1"else print(0)
A shorter one:
input()=="1"and{print(end="1")for _ in iter(set,0)}or print(0)
Brainfuck interpreter
(setattr(__builtins__,'i',input),
setattr(__builtins__,'p',i()),
setattr(__builtins__,'c',0),
setattr(__builtins__,'l',[0]),
setattr(__builtins__,'r',[0]),
setattr(__builtins__,'s',lambda l,i,e:l.__setitem__(i,e)),
setattr(__builtins__,'a',lambda l,e:l.append(e)),
[(setattr(__builtins__,'o',p[c]),
setattr(__builtins__,'ll',len(l)-1),
setattr(__builtins__,'lr',len(r)-1),
setattr(__builtins__,'lf',l[-1]),
setattr(__builtins__,'rf',r[-1]),
(
(
(o=='+')and s(l,ll,(lf+1)%256)),
(o=='-')and s(l,ll,(lf-1)%256),
(o=='<')and(a(r,lf),(ll>0)and l.pop()),
(o=='>')and(a(l,rf),(lr>0)and r.pop()),
(o=='.')and(print(chr(lf),end='',flush=1)),
(o==',')and(l.pop(),a(l,ord(i()or chr(lf))%256)),
setattr(__builtins__,'c',c+1)
)
if not o in '[]'else
(setattr(__builtins__,'b',c),
setattr(__builtins__,'j',1),
setattr(__builtins__,'m',[-1,1][o=='[']),
[(setattr(__builtins__,'b',b+m),setattr(__builtins__,'n',p[b]),
(n=='[')and setattr(__builtins__,'j',j+m),
(n==']')and setattr(__builtins__,'j',j-m)
)
for _ in iter(lambda:j,0)],
setattr(__builtins__,'c',b)if [lf,lf==0][(m+1)//2] else setattr(__builtins__,'c',c+1)
)
)
for _ in iter(lambda:len(p)>c,0)]
)