[Python-ideas] Extending language syntax
Gregory Salvan
apieum at gmail.com
Mon Nov 11 12:20:57 CET 2013
Hi all,
I've just joined you, I hope I'll not make you loose your time with
questions already covered.
Sorry, if my suggest represents lot of work (certainly too much) and about
my lack of knowledge about internal implementation, but I hope opening a
discussion can help.
I would suggest to introduce a new statement to provide a way to extend
language syntax.
Abstract:
While reading this article about None
history<http://python-history.blogspot.fr/2013/11/story-of-none-true-false.html>I
realised I was not alone to wonder about some aspect of the language.
Some of these are:
- constant and immutability
- null object and "None is a singleton"
- readability of code
*Constant:*
It's a convention, no matter with that.
My question is more if there is no memory overuse to have fixed values that
behave as variable.
*Null Object:*
NoneType is not overridable.
Having null objects instance of NoneType wouldn't make sense ?
Can't assign keywords so "None is a singleton".
Wouldn't it be more simple to have "keyword" type and be able to assign a
keyword to a variable ?
With an object hierarchy like: keyword <- ReservedKeyword
I find it can make clearer why some keywords are reserved without changing
actual behaviour.
*Readability of code:*
I have shouldDsl <http://www.should-dsl.info/> use case in mind.
The introduction illustrate my thought:
"*The goal of Should-DSL is to write should expectations in Python as clear
and readable as possible, using an “almost” natural language (limited by
some Python language’s constraints).*"
I find ShouldDsl perturbing, as the aim is laudable but the implementation
stays a hack of the language.
I see the influence of functional paradigm in this syntax and agreed with
the fact it becomes a "must have".
I love Python syntax and find, it has very few limits, but maybe it can be
improved by reducing some constraints.
One concerns in extending syntax is that the language can evolve on users
usage.
I see it like an open laboratory where developpers experiments new keywords
like "should" implemented in their favorite language and when this keyword
is largely accepted by the community it can finally integrate the core.
*A solution*:
I was first thinking of a statement like "Instance" to declare an immutable
object with a single instance (like None), but I then consider how it would
be great to be able to extend the syntax.
I thought the keyword "Keyword", which exists in clojure for
example<http://clojure.org/data_structures#Data%20Structures-Keywords>,
can do the job.
A first implementation can be:
Keyword <name>(<keyword, literal or object>):
field = <something> # fixed value: self.field = "something" will raise
an error
def behaviour(self, *args, **kwargs):
# do something
def __get__(self, left, right, block_content):
# what to do when accessing keyword
Inline implementation:
Keyword <name>(<keyword, literal or object>)
*Examples of use:*
*Constant*:
Keyword Pi(3.14159)
*Null Object* and immutable objects:
Class Person(object):
def __init__(self, name):
self.name = name
JohnDoe = Person(name="anonymous")
Keyword Anonymous(None, JohnDoe)
Anonymous.name = "JohnDoe" # raise an error
assert isinstance(Anonymous, type(None)) # is true
As Anonymous is immutable , it is also thread safe.
The mecanism which consist of freezing an object can be applied in other
situation.
An interesting approach can be to define keywords identity throught a hash
of their value, like value object.
*Should keyword*:
Keyword ShouldEqual(keyword):
def __get__(self, left, right, block):
if left != right:
raise AssertError()
### weird examples:
*Multiline Lambdas*:
Keyword MultilineLambda(keyword):
def __get__(self, left, right, block_content):
def multiline_lambda(*args, **kwargs):
self.assert_args_valid(right, args, kwargs) # compare "right"
(tuple) with args and raise exception in case not valid
return self.run(block_content, args, kwargs) # "run" is a shortcut
because I don't know which type is the most appropriate for block content
return multiline_lambda
divide = MultilineLambda a, b:
if b == 0:
raise MyDivideByZeroError()
return a/b
*Reimplementing else*:
Keyword otherwise(keyword):
def __get__(self, left, right, block):
if left: return # left is the result of "if" block
self.run(block)
if something:
# do something
otherwise:
# other
Whereas this example is weird, it seems to me that we can better figure out
what language do.
Thanks all to have kept python open and give us opportunities to freely
discuss about its future implementation.
Have a nice day,
Grégory
-------------- next part --------------
An HTML attachment was scrubbed...
URL: <http://mail.python.org/pipermail/python-ideas/attachments/20131111/db33b19d/attachment.html>
More information about the Python-ideas
mailing list