Index ¦ Archives > Tag: language hacks

Adding multi-line lambdas to Python

At the risk of this blog becoming dedicated to "the worst things you can do in Python" I present to you: multiline lambdas in Python!

Implementation was simple:

import sys

def fun(string):
    frame = sys._getframe(1)
    out = {}
    locals = {}
    locals.update(frame.f_globals)
    locals.update(frame.f_locals)
    exec ("def __anon …

Adding functional style pattern matching to Python

One of the nicest features of functional programming languages like Haskell and Erlang is pattern matching. An example, from Learn You Some Erlang:

greet(male, Name) ->
    io:format("Hello, Mr. ~s!", [Name]);
greet(female, Name) ->
    io:format("Hello, Mrs. ~s!", [Name]);
greet(_, Name) ->
    io:format("Hello, ~s!", [Name]).

The …

© Chad Selph. Built using Pelican. Theme by Giulio Fidente on github.