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 6:56 ID:0nchLgE3

>>35
Thanks to the power of libraries you can make C++ (or any other language) do a backflip and eat apple pie.
So, can you make C++ do dynamic typing, first-class functions and classes, generators, list comprehensions, etc.? Yes, in fact you can. That library is called Python. You can embed Python in C (or C++) to do that.

>>37
As an aside: it's for this reason that I don't understand why Lisp seems stuck in the stone-age (hello Common Lisp!). They're all too busy wanking over their superiority to come up with a single canonical distro with a modern "batteries included" standard library.
Fucking truth. I'd also clean all the legacies Common Lisp has, and that 2-Lisp stupidity. Basically, I'd do what you say but start with Scheme, or even a clean Scheme, plus an awesome macro system and syntax for indexing arrays and dictionaries.

>>38
Ok, I'll try enumerating some features of Python that C doesn't have.

- Dynamic typing (AKA duck typing) with strict types
- Ubiquitous polymorphism (method calls are evaluated in real time, if a method with that name exists it's called, don't give a shit about classes)
- Garbage collection
- Built-in, real, properly defined Unicode support
- Built-in lists (and tuples, read-only lists)
- Built-in dictionaries (hash tables) which can use any immutable key (even tuples!)
- Built-in sets with set operations
- Powerful list slicing (like [a:b:c], to access elements from [a to b) step c)
- List comprehensions
- Functional programming tools
- First-class functions
- First-class classes
- Properties that look and feel like properties but are actually functions: forget that getter and setter faggotry
- Metaclasses
- Functions and classes are closures
- Anonymous functions/closures (AKA lambda expressions)
- Functions can return more than one value (tuples)
- Generators
- Coroutines
- Keyword arguments
- Function application with lists and dictionaries
- Introspection, giving access to object and types dictionaries, backtraces (tracebacks), stack frames, variable dictionaries and debugging facilities, all from the language
- Fully dynamic: *anything* you can do on write-time you can do on run-time, including defining new functions, classes, inheritance rules (mixins), etc., and you can also redefine functions to change or hot-upgrade a program's behaviour without restart
- Dynamic code evaluation: eval and exec execute Python code that's in a string
- Direct access to bytecode and AST editing (though it's fugly)

And I'm probably missing 10-15 awesome features C++ lacks.

If I were you, after reading all of these features C++ can't even dream of I'd start learning Python right now. You can do it in 2 days, then discover how to use all of this with experience as you use it here and there.

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