Slide 1

Slide 1 text

WHAT PYTHON CAN LEARN FROM OTHER LANGUAGES Noah Kantrowitz - coderanger.net 1 North Bay Python 2024 – Noah Kantrowitz – @coderanger@cloudisland.nz

Slide 2

Slide 2 text

NOAH KANTROWITZ • He/him • coderanger.net | cloudisland.nz/@coderanger • Kubernetes and Python • SRE/Platform for Geomagical Labs, part of IKEA • We do CV/AR for the home 2 North Bay Python 2024 – Noah Kantrowitz – @coderanger@cloudisland.nz

Slide 3

Slide 3 text

PYTHON 3 North Bay Python 2024 – Noah Kantrowitz – @coderanger@cloudisland.nz

Slide 4

Slide 4 text

PYTHON PYTHON IDEAS But not the mailing list 4 North Bay Python 2024 – Noah Kantrowitz – @coderanger@cloudisland.nz

Slide 5

Slide 5 text

PHP 5 North Bay Python 2024 – Noah Kantrowitz – @coderanger@cloudisland.nz

Slide 6

Slide 6 text

PHP DEPLOYMENT • Drag and drop • foo.php → http://example.com/foo.php • Deep editor integration 6 North Bay Python 2024 – Noah Kantrowitz – @coderanger@cloudisland.nz

Slide 7

Slide 7 text

AUTOLOADERS • new \Foo\Bar(1, 2, 3) • Foo\Bar.php • namespace Foo; class Bar { } • No require required "autoload": { "psr-4": {"Foo\\": "src/"} } 7 North Bay Python 2024 – Noah Kantrowitz – @coderanger@cloudisland.nz

Slide 8

Slide 8 text

STANDARD LIBRARY • Batteries Included ... for a speci!c niche • Database libraries • Libcurl • PECL as a secondary stdlib 8 North Bay Python 2024 – Noah Kantrowitz – @coderanger@cloudisland.nz

Slide 9

Slide 9 text

TEMPLATING Hello

and some more HTML<"p> <"div> 9 North Bay Python 2024 – Noah Kantrowitz – @coderanger@cloudisland.nz

Slide 10

Slide 10 text

RUBY 10 North Bay Python 2024 – Noah Kantrowitz – @coderanger@cloudisland.nz

Slide 11

Slide 11 text

SYMBOLS • :foo ('foo in LISP #foo in Smalltalk) • sys.intern("foo") • Strings with bene!ts • myfn(foo=1), maybe MyStrEnum.FOO 11 North Bay Python 2024 – Noah Kantrowitz – @coderanger@cloudisland.nz

Slide 12

Slide 12 text

BLOCKS foo do |x| x + 1 end def _inner(x): return x + 1 foo(_inner) 12 North Bay Python 2024 – Noah Kantrowitz – @coderanger@cloudisland.nz

Slide 13

Slide 13 text

METHOD CHAINING y = x.one do |val| .." end.two do |val| .." end def _one(val): ..# def _two(val): ..# y = x.one(_one).two(_two) 13 North Bay Python 2024 – Noah Kantrowitz – @coderanger@cloudisland.nz

Slide 14

Slide 14 text

TANGENT: DART METHOD CASCADING a.b()."c() ."d(); x = a.b() x.c() x.d() 14 North Bay Python 2024 – Noah Kantrowitz – @coderanger@cloudisland.nz

Slide 15

Slide 15 text

BUILDERS x = Foo.new do |cfg| cfg.bar = 1 if other cfg.baz(2, 3) end do # What if? x = with Foo as cfg: cfg.bar = 1 if other: cfg.baz(2, 3) 15 North Bay Python 2024 – Noah Kantrowitz – @coderanger@cloudisland.nz

Slide 16

Slide 16 text

RUBY FOR GOOD • Court Appointed Special Advocates • National Diaper Bank Network • Chicago Tool Library • Habitat For Humanity 16 North Bay Python 2024 – Noah Kantrowitz – @coderanger@cloudisland.nz

Slide 17

Slide 17 text

TYPESCRIPT (AND JAVASCRIPT) 17 North Bay Python 2024 – Noah Kantrowitz – @coderanger@cloudisland.nz

Slide 18

Slide 18 text

NULL COALESCING SAFE NAVIGATION x ?" y x?#foo?#bar() y if x is None else x x.foo.bar() if x is not None and x.foo is not None else None 18 North Bay Python 2024 – Noah Kantrowitz – @coderanger@cloudisland.nz

Slide 19

Slide 19 text

OBJECTS AND INLINE TYPES function foo(x: {bar: string}) { baz(x.bar); } # What if? def foo(x: TypedDict["bar": str]): baz(x["bar"]) 19 North Bay Python 2024 – Noah Kantrowitz – @coderanger@cloudisland.nz

Slide 20

Slide 20 text

PROMISES (AND FUTURES) fetch(`/search`) .then(resp =" resp.json()) .then(data =" fetch(`/detail/${data.id}`)) .then(resp =" resp.json()) .then(data =" console.log(data.name)); async def foo(): resp = await fetch("/search") data = await resp.json() resp = await fetch(f"/detail/{data['id']}") 20 North Bay Python 2024 – Noah Kantrowitz – @coderanger@cloudisland.nz

Slide 21

Slide 21 text

GO 21 North Bay Python 2024 – Noah Kantrowitz – @coderanger@cloudisland.nz

Slide 22

Slide 22 text

GOROUTINES AND CHANNELS messages :" make(chan string) go func() { messages #- "ping" }() msg :" #-messages # Soon? messages = Channel[str]() def func(): messages.append("ping") subinterpreter.run(func) msg = messages.pop() 22 North Bay Python 2024 – Noah Kantrowitz – @coderanger@cloudisland.nz

Slide 23

Slide 23 text

INTERFACE CASTS type Foo interface { Bar(int) int } y, ok = x.(Foo) y.Bar(1) @typing.runtime_checkable class Foo(typing.Protocol): def bar(self, x: int) -# int: ..% class Impl: def bar(self, x: str) -# str: ..% x = Impl() isinstance(x, Foo) =' True x.bar(1) # Oops wrong type 23 North Bay Python 2024 – Noah Kantrowitz – @coderanger@cloudisland.nz

Slide 24

Slide 24 text

STATIC COMPILATION • GOOS=windows GOARCH=amd64 go build • FROM scratch • ./myapp • xkcd://1987 24 North Bay Python 2024 – Noah Kantrowitz – @coderanger@cloudisland.nz

Slide 25

Slide 25 text

25 North Bay Python 2024 – Noah Kantrowitz – @coderanger@cloudisland.nz

Slide 26

Slide 26 text

PERL PIE perl -pi -e 's/VAR/Hello/g' input.txt What if? python -m pie 're.sub("VAR", "Hello")' input.txt 26 North Bay Python 2024 – Noah Kantrowitz – @coderanger@cloudisland.nz

Slide 27

Slide 27 text

C# AND LINQ int[] scores = [97, 92, 81, 60]; IEnumerable scoreQuery = from score in scores where score > 80 select score; • Dataframes • DuckDB 27 North Bay Python 2024 – Noah Kantrowitz – @coderanger@cloudisland.nz

Slide 28

Slide 28 text

SCHEME AND CONTINUATIONS (call/cc (lambda (c) (c 42))) =" 42 (call/cc (lambda (c) (+ 1 (c 42)))) =" 42 def call_cc(f): ex = type('Continuation', (Exception,), {}) def c(val): raise ex(val) try: return f(c) except ex as exc: return exc.args[0] def foo(c): c(42) call_cc(foo) =# 42 28 North Bay Python 2024 – Noah Kantrowitz – @coderanger@cloudisland.nz

Slide 29

Slide 29 text

SEASIDE changeBackgroundColor |color| color :" self call: ColorPicker new. " ^ Respond to client and then wait for another request" blog backgroundColor: color 29 North Bay Python 2024 – Noah Kantrowitz – @coderanger@cloudisland.nz

Slide 30

Slide 30 text

TANGENT: SMALLTALK & FLOW CONTROL class True: def if_true(self, f): return f() def if_false(self, f): pass class False: def if_true(self, f): pass def if_false(self, f): return f() def _inner(): print("Yes") x.if_true(_inner) value = 0 def _other(): x = value <# 10 def _inner(): value += 1 x.if_true(_inner) return x y = True() y.do_while(_other) 30 North Bay Python 2024 – Noah Kantrowitz – @coderanger@cloudisland.nz

Slide 31

Slide 31 text

EVEN MORE TANGENT: FORTH \ quit is the top level preForth interpreter loop. It reads tokens \ and handles them until an error occurs or the input is exhausted. : quit ( -" ) token \ get next token \ run interpreters ?: \ :-definition ?code \ code definitions ?pre \ pre* ?\ \ comment dup ?exit drop \ unhandled or EOF tail quit ; \ cycle 31 North Bay Python 2024 – Noah Kantrowitz – @coderanger@cloudisland.nz

Slide 32

Slide 32 text

ERLANG AND OTP SUPERVISORS • Task exception was never retrieved – Oops! init(_Args) -" SupFlags = #%strategy =' one_for_one, intensity =' 1, period =' 5}, ChildSpecs = [#%id =' myapp, start =' {myapp, start_myapp, []}, restart =' permanent, shutdown =' brutal_kill, type =' worker, modules =' [myapp]}], {ok, {SupFlags, ChildSpecs}}. 32 North Bay Python 2024 – Noah Kantrowitz – @coderanger@cloudisland.nz

Slide 33

Slide 33 text

SOME DAY: PY OTP? async with loop.sup() as s1: c = s1.create_task(cache_manager()) async with s1.sup(on_error=emit_log) as s2: s2.create_task(web_server(c)) async with loop.sup(watchdog=5) as s3: s3.create_task(cleanup()) 33 North Bay Python 2024 – Noah Kantrowitz – @coderanger@cloudisland.nz

Slide 34

Slide 34 text

WHERE DO WE GO FROM HERE 34 North Bay Python 2024 – Noah Kantrowitz – @coderanger@cloudisland.nz

Slide 35

Slide 35 text

MACROS! macro_rules! add_as{ ($a:expr, $b:expr, $typ:ty) =" { $a as $typ + $b as $typ } } (defmacro setq2 (v1 v2 e) (list 'progn (list 'setq v1 e) (list 'setq v2 e))) 35 North Bay Python 2024 – Noah Kantrowitz – @coderanger@cloudisland.nz

Slide 36

Slide 36 text

PEP 638 36 North Bay Python 2024 – Noah Kantrowitz – @coderanger@cloudisland.nz

Slide 37

Slide 37 text

ABSTRACT SYNTAX TREES expr = BoolOp(boolop op, expr* values) | NamedExpr(expr target, expr value) | BinOp(expr left, operator op, expr right) | UnaryOp(unaryop op, expr operand) | Lambda(arguments args, expr body) | IfExp(expr test, expr body, expr orelse) | Dict(expr* keys, expr* values) ..# 37 North Bay Python 2024 – Noah Kantrowitz – @coderanger@cloudisland.nz

Slide 38

Slide 38 text

S-EXPRESSIONS • An atom • a • A pair of s-expressions • (a . b) (* 1 (+ 2 3)) _______ / ___\___ * / __\__ 1 / _\_ + / \ 2 3 38 North Bay Python 2024 – Noah Kantrowitz – @coderanger@cloudisland.nz

Slide 39

Slide 39 text

LISP • (defmacro addone (x) (list '+ '1 x)) • Oh look, atom 'defmacro, record this into the list of macros • (* 1 (addone (- 2 1))) • Oh look, atom 'addone is a macro, do the macro thing! • Execute (list '+ '1 x) • Got back ('+ '1 ('- '2 '1)) • (* 1 (+ 1 (- 2 1))) 39 North Bay Python 2024 – Noah Kantrowitz – @coderanger@cloudisland.nz

Slide 40

Slide 40 text

METAPROGRAMMING • Tokenize → Parse → Compile → Execute • Instead: Parse → Macro Compile → Macro Execute → Parse 40 North Bay Python 2024 – Noah Kantrowitz – @coderanger@cloudisland.nz

Slide 41

Slide 41 text

RUST • Procedural macros • Custom #[derive] • Attribute-like #[proc_macro_attribute] • Function-like #[proc_macro] • pub fn foo(input: TokenStream) -> TokenStream { • Declarative macros – macro_rules! 41 North Bay Python 2024 – Noah Kantrowitz – @coderanger@cloudisland.nz

Slide 42

Slide 42 text

MACRO_RULES! macro_rules! vec { ( $( $x:expr ),* ) =" { { let mut v = Vec:$new(); $( v.push($x); )* v } }; } let myvec: Vec = vec![1, 2, 3]; /" Becomes ..$ let myvec: Vec = { let mut v = Vec:&new(); v.push(1); v.push(2); v.push(3); v }; 42 North Bay Python 2024 – Noah Kantrowitz – @coderanger@cloudisland.nz

Slide 43

Slide 43 text

WHAT TO TAKE FROM ALL THIS? 43 North Bay Python 2024 – Noah Kantrowitz – @coderanger@cloudisland.nz

Slide 44

Slide 44 text

THANK YOU Find Me Later For Questions 44 North Bay Python 2024 – Noah Kantrowitz – @coderanger@cloudisland.nz

Slide 45

Slide 45 text

PHP Ease of deployment Auto load and its deep integration with Composer Expansive std lib Ruby Easy lambdas via blocks Flow programming Symbol literals Builder pattern Go Static compilation and FROM scratch Interfaces 45 North Bay Python 2024 – Noah Kantrowitz – @coderanger@cloudisland.nz