Skip to content

Instantly share code, notes, and snippets.

@0riginaln0
Created March 8, 2026 07:26
Show Gist options
  • Select an option

  • Save 0riginaln0/33542a2be9d3982a7e3d0bb325864a62 to your computer and use it in GitHub Desktop.

Select an option

Save 0riginaln0/33542a2be9d3982a7e3d0bb325864a62 to your computer and use it in GitHub Desktop.
MoonScript and Python comparison
------------------
-- Basic Syntax --
------------------
if condition
do_something!
do_another_thing!
else
do_different_thing!
--------------------------
-- Function Definitions --
--------------------------
-- Basic function
add = (x, y) -> x + y
-- Fat arrow (includes self)
method = (x) => @value + x
-- Default parameters
greet = (name="World") -> "Hello #{name}!"
-------------
-- Classes --
-------------
class Vehicle
new: (@name, @speed) =>
@running = false
start: =>
@running = true
print "#{@name} started"
class Car extends Vehicle
new: (@wheels=4) =>
super "Car", 60
---------------------
-- Data Structures --
---------------------
-- Array-like table
items = {1, 2, 3, 4}
-- Key-value pairs
person = {
name: "John"
age: 30
city: "New York"
}
-- Implicit table (no braces needed)
config =
debug: true
timeout: 5000
--------------------------
-- String Interpolation --
--------------------------
name = "World"
greeting = "Hello #{name}!" -- String interpolation
multiline = "This string
spans multiple lines"
-------------------
-- Comprehensions --
-------------------
-- List comprehension
doubled = [item * 2 for item in *items]
filtered = [item for item in *items when item > 5]
-- Table comprehension
lookup = {item, item^2 for item in *numbers}
-------------------------------------
-- Assignment and Update Operators --
-------------------------------------
x += 10
s ..= "world"
b and= true or false
-------------------------
-- Web App hello world --
-------------------------
lapis = require "lapis"
class App extends lapis.Application
"/hello": => "Hello World!"
[hello_admin: "/admin"] => "Hello Admin"
"/dashboard": =>
@html ->
a href: @url_for("hello_admin"), "Admin Panel"
# ============
# Basic Syntax
# ============
if condition:
do_something()
do_another_thing()
else:
do_different_thing()
# ====================
# Function Definitions
# ====================
# Basic function
def add(x, y):
return x + y
# Method with self
def method(self, x):
return self.value + x
# Default parameters
def greet(name="World"):
return f"Hello {name}!"
# =======
# Classes
# =======
class Vehicle:
def __init__(self, name, speed):
self.name = name
self.speed = speed
self.running = False
def start(self):
self.running = True
print(f"{self.name} started")
class Car(Vehicle):
def __init__(self, wheels=4):
super().__init__("Car", 60)
self.wheels = wheels
# ===============
# Data Structures
# ===============
# List
items = [1, 2, 3, 4]
# Dictionary
person = {
"name": "John",
"age": 30,
"city": "New York"
}
# No equivalent to implicit tables
# ====================
# String Interpolation
# ====================
name = "World"
greeting = f"Hello {name}!" # f-string interpolation
multiline = """This string
spans multiple lines"""
# ==============
# Comprehensions
# ==============
# List comprehension
doubled = [item * 2 for item in items]
filtered = [item for item in items if item > 5]
# Dictionary comprehension
lookup = {item: item**2 for item in numbers}
# ===============================
# Assignment and Update Operators
# ===============================
x += 10
s += "world"
b &= True or False
# ===================
# Web App hello world
# ===================
from flask import Flask
app = Flask(__name__)
@app.route("/hello")
def hello():
return "Hello World!"
@app.route("/admin")
def hello_admin():
return "Hello Admin"
@app.route('/dashboard')
def dashboard():
return f'<a href="{url_for("hello_admin")}">Admin Panel</a>'
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment