Skip to content

Instantly share code, notes, and snippets.

@fletom
Created June 18, 2011 07:12
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save fletom/1032883 to your computer and use it in GitHub Desktop.
Save fletom/1032883 to your computer and use it in GitHub Desktop.
A crazy Python syntax change that reduces the number of keywords and makes assignment usually done with =
# Some people have said that this import function violates the nature of functions by modifying it's caller's globals
# I would say that with such an essential as import an exception can be made. In any case, practicality over purity, and
# import('x') is a lot more practical and clean than x = import('x')
import django
# becomes
import('django')
import django.submodule
# becomes
import('django.submodule')
# Though this is considered bad style...
import django.submodule, os.path
# becomes
import(['django.submodule', 'os.path'])
from django import *
# becomes
import('django.*')
from django import submodule
# becomes
import('django.', ['submodule_one'])
from django import submodule_one, submodule_two
# becomes
import('django.', ['submodule_one', 'submodule_two'])
# The import_as function would need to be renamed, as it doesn't read nicely ("import django as d" makes more sense than "d import as django").
# Any ideas?
# Also, this function is close to the current __import__()
import django as d
# becomes
d = import_as('django')
from os import path as p
# becomes
p = import_as('os.path')
from django import submodule_one as a, submodule_two as b
# becomes
a, b = import_as('django.', ['submodule_one', 'submodule_two'])
def foo(x):
pass
# becomes
foo = function(x):
pass
class SomeClass(superclass):
pass
# becomes
SomeClass = class(superclass):
pass
# There is some debate about whether or not whitespace should be significant in expressions, but when you actually read it this is rather elegant.
# Again, Python is unique in that it forces good style when normally one could get away without it. This is a clean and simple way to do that.
# Since functions would be assigned using =, all functions are anonymous and we could do things Javscript-style e.g.:
Q('.class').each((function(i):
pass
))
# Or for really small things
Q('.class').each((function(i): pass))
# And this seems strange, but you could:
(function():
pass
)()
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment