Return Styles: Pseud0ch, Terminal, Valhalla, NES, Geocities, Blue Moon. Entire thread

How would you improve Python?

Name: Anonymous 2007-07-09 12:37 ID:V5liB5dq

How would you improve Python? I want to picture the perfect practical, multi-paradigm language. I decided to start with Python because I think it's the easiest one to fix. Lua would be another good candidate. So this is what I'd do to Python:

- def and class become expressions and specifying a function class or name is optional. They still maintain the forced indentation of code, and can be used as follows:
function = def (x, y): #Indentation level = 0
    x + y #Indentation level = 4, inside the def
print... #Indentation back to 0, thus def ended, and assignment ended

tuple_of_functions = (def (x, y): #Indentation level = 0
    x + y #Indentation level = 4, inside the def
, lambda x: -x) #Indentation back to 0, thus def ended, assignment did not because of parens


(Note that you can wrap def and class around extra parens to use like braces if you don't like that comma there.)

- $ becomes shorthand notation for lambda, so $x: x + 1 is the same as lambda x: x + 1 . Furthermore, ${ ... } becomes a shorthand for def ():, and supports nested braces, e.g.:
f = ${
           x = 0 #The forced indentation of code begins at whatever column the contents of ${ start
           if x:
               do_something(${ ... })
     }, 1, 2, 3, ${ ... } #f is a tuple of <lambda>, 1, 2, 3, <lambda> 
The closing brace may appear anywhere. If you start the contents of ${} in the same line, as in ${ x + 1 }, you are limited to a single line.

- Besides the forced indentation of code, there's the forced tail call optimization in the language specification, so any interpreter must do it.

- if, while, for, with and try become expressions which return the latest expression they evaluated, and can be used anywhere. A def function returns the last expression evaluated, so the return keyword is optional when specified at the end of a function. break and continue become (special) expressions that return nothing and are only valid within while. print, import, exec and assert become functions. raise becomes a expression. del, return and yield become special expressions. global becomes an expression but it'll be deprecated. pass is deprecated, and becomes a synonym of None. Assignment statements will disappear in a later suggestion. Thus no statements remain. Introducing the forced expressions.

- New while-like expression: do: (forced indentation of code) ... (dedent) while condition (like C's do { } while ();)

- Assignment operations return the assigned value, as in C.

- ++ and -- operators, as in C. They call methods __inc__() and __dec__(), and return the original or the modified value depending on whether they appear before or after the variable.

- Incompatible change: if you assign to a variable in some way (like using = or def), you create a new variable in the local function, as you do in Python, but only if this variable doesn't exist in an outer lexical scope. If it does, then the outer variable is modified. To specify the forced creation of a local variable with the same symbol as an outer lexical scope, you use the local special expression, prefixing the variable name with "local" anywhere in the function, e.g.:
[code]def f():
    i, j = 1, 1
    (def ():
        i, local j = 2, 2
        #local j++ would have caused an exception, as j did not exist and ++ requires that it exists, like j += 1
    )()
    print i, j #Expected: 2, 1


- Braces quote strings like Perl's q{} operator. They can be nested, so {a{b}} is the same string as 'a{b}'.

- .. is a new operator. Used with ints, it generates xrange objects, e.g. 3..10 returns xrange(3, 11). Used with other objects, it calls __range__(other) or __rrange__(other).

- All the changes coming in Python 3000 regarding dictionary functions, deprecated functions, etc. should be applied too.


So, what do you think? Did I screw up somewhere? These changes would allow better functional programming with Python, and something close to macros or TCL commands, e.g. you could define an iff(c, t, f) macro which doesn't evaluate both t and f like:
def iff(c, t, f):
    t() if c() or f()


and you could use it like:

iff(${ x == 3 },
          ${ do_something... },
          ${ do_something_else... })


Assignment would work, there's multiline ${}, etc.

Name: Anonymous 2007-07-12 8:27 ID:RQ/JS02Q

>>43

Don't worry, many of those features are planned to be introduced in the new C2009 standard. It's not the C your grandma programmed.

Newer Posts
Don't change these.
Name: Email:
Entire Thread Thread List