Eike Welk wrote:
On Saturday 18.12.2010 03:03:06 Greg Ewing wrote:
Eike Welk wrote:
| {| a, b |} -> # Attribute existence and access
is far too cryptic and does nothing to improve readability.
List comprehensions are also cryptic if you see them for the first time. The curly brackets with bars "{| |}" should symbolize that Python object are glorified dicts. It is also quite close to Ocaml's syntax for records.
Not really. A list comp looks like a cross between a list and a for-loop, which is exactly what a list comp is. [2*x for x in sequence] I believe that people -- at least some people -- could intuit the meaning of this from context. But even if they can't, it's a simple extension to existing Python syntax they should already know. And best of all, you can easily experiment on it by copying and pasting a list comp into the interactive interpreter and seeing what it does. Your proposed syntax {| |} could be related to sets, or dicts, or both. Or it could be related to the | operator. If you don't know which, it's hard to guess. It looks more like a set than a dict: {a, b} # set {a: value, b: value} # dict {|a, b|} # looks like a set with extra symbols and there's probably nothing you can do with it in isolation from a full pattern match block.
My secret agenda was however, to later introduce a new class and object syntax for Python. I just didn't dare to propose a completely revised Python. But here we go: :-)
Class Creation --------------
Foo = class {| a, b |}
This should be equivalent to:
class Foo(object): def __init__(self, a, b): self.a = a self.b = b
What's so special about Foo that it needs special syntax just to make it easier? In any case, these days I'd suggest that's probably best written as a namedtuple:
from collections import namedtuple Spam = namedtuple('Spam', 'a b c') x = Spam(a=23, b=42, c=None) x Spam(a=23, b=42, c=None)
Inheritance is not so important in the context of classes that have no methods. It could be expressed with a new method ("inherits") of the metaclass. Like this:
Foo = class {| a, b |}.inherits(Bar, Baz)
How one would create methods, and if it should be possible to create method at all, needs to be discussed.
I don't think it does :) I think you're falling into the trap of thinking that everything needs to be a one-liner. It doesn't.
Instance Creation -----------------
foo = {| a=1, b=2 |}:Foo
This should be equivalent to:
foo = Foo(a=1, b=2)
Why do you think you need two ways of spelling Foo(a=1, b=2)? What's wrong with the Python syntax for it? It seems to me that you want the obfuscation of OCAML's syntax with the slowness of Python, a strange choice...
In the usual case, it should not be necessary to specify the class name:
foo = {| a=1, b=2 |}
The run-time should search for a class with a matching "__init__" method. In
Oh wow. Just ... wow. You want the Python virtual machine to do a search through N objects in K scopes, checking each one to see if it is a class, then checking the signature of the __init__ method, just to allow the caller to *implicitly* specify the class of an instance instead of explicitly. -1000 on that.
case of ambiguities an exception would be raised. You have to name object attributes unambiguously for this to work. However it is more flexible than Ocaml because you can disambiguate it by specifying the class name.
These constructions are expression, and can therefore be nested.
*cries* -- Steven