Skip to content

Instantly share code, notes, and snippets.

@jeremyBanks
Created October 21, 2009 00:21
Show Gist options
  • Save jeremyBanks/214737 to your computer and use it in GitHub Desktop.
Save jeremyBanks/214737 to your computer and use it in GitHub Desktop.
[2010-01] rambling in my head on what a python could do
_Sorry if this is poorly-articulated, I'm a bit drowsy at the moment._
I know that Python is supposed to have an easy-to-read syntax, and appreciate
that allowing too much flexibility could have some effect on that. I don't see this as a replacement for Python's syntax, just as an alternative, maybe a fork or an interpreter flag.
I like Python's syntax, but would like it to be more flexible. Using ideas
from other languages, here's the approach I was thinking of.
Looking at a simple function decoration:
def foo(x, y):
return(bar(x, y))
this peice of that:
(x, y):
return(bar(x, y))
I'd have this be valid code on its own, such that it could be used in
an expression as a "multi-line lambda" (lambdas wouldn't exist after
this). I'd also make allow implicit returns, but I realize that would
break existing programs (perhaps an interpreter flag?). Anyway, here's
something I'd like to see possibe:
foo = x: bar(x)
y = foo 5
print(y)
This would be considered very bad style, but shows the mechanisms. the
whatever precedes the colon is the argument to the function. if you
have a tuple of argument identifiers (such as "(x, y)") and you pass
an argument to the function, it would behave exactly as it does
now. however, if you only have one identifier, you can things without
parentheses (unless you actually mean to pass a tuple). (You can also pass
a tuple in place of a parenthesized argument list to a
tuple/multi-argument function.) This allows you to create the def keyword as just another function, used in the same way but not limited to identifying functions:
def identifier value
which you could use with the function syntax so that current current
python function declarations make sense.
def identifier value
and
identifier = value
would not work exactly the same, as `def` also sets the `__name__`
attribute on the value to the identifier. I can't remember how python
stack frames work right now, so pretend that `sys.get_what_i_want`
returns the dictionary of the namespace that the function is being called from, and `def`
could be defined using curried functions something like this:
def = identifier : value : sys.get_what_i_want()[identifer] = value
value.__name__ = identifier
I think this would be a wonderful amount of flexibility to have. Throwing
in some way to use a different type for the namespace dictionary
like you can with metaclasses would be great as well. you'd be able to
bootstrap most of the existing language syntax through this and it would
make this syntax a lot more powerful.
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment