[Python-Dev] Extended Function syntax
Roman Suzi
rnd@onego.ru
Sun, 2 Feb 2003 12:29:35 +0300 (MSK)
code_thunk = ```
if Guido.let_it_fly():
print "This is my syntax for inline code block"
```
Now I understood the summary of this thread for myself. It's all about making
extended lambdas in Python so Python could manipulate code as it does with
other objects! Now I am convinced this is great idea and quite Pythonic too.
Also if I understood correctly, the main problem is namespace interaction.
My thought here is that it shouldnot be different to lambdas:
environment need to be visible in the inside.
Another problem is with exceptions. But is it really a problem?
How different it is when we exec something?
So, the problem (IMHO) has this subproblems:
1. How to express an arbitrary codeblock object anywhere in the expression?
2. What protocol to use to communicate with this codeblock?
Let's see what we already have in Python:
I. case: user-defined function
1. def-statement
2. arguments and one value after return statement.
Environment of place of definition available.
II. case: lambda function
1. lambda-operator
2. arguments and a single return value. Environment available.
III. case: compiled code block
1. any string and then compile() or directly exec(), etc
2. globals(), locals()
Now:
IV. case: inline code block
aka extended lambda ;-)
1. special (probably ':'-powered) syntax, allowing to insert code
snippet into any place in the expression
2. this is a big question. I propose to use some functions around
codeblock which will add needed namespaces, exception handlers
and other things needed for code snippet to work.
BTW, it's my understanding that is such construct (inline code block) will be
added to Python, then almost we will be able to model ANY Python control-flow
operator with it! Isn't it really cool?
And of course inline code snippet object will be extensible objects, allowing
for any kinds of modifications, adding aspects (for those who uses aspect
programming), pre/post conditions, currying, locks, security control, etc,
etc. It will be a new era of Python development. It will nearer Python
to LISP, where code and data are same.
I am sure there is no problem defining
+, *, % operations for them (obvious, isn't it?):
+ - make a sequence
* - make a for _ in range(N) cycle,
% - provide parameters (with a dict or whatever), locals() for example.
(Am i dreaming?)
As for optimisations, drop them, Guido, for now: inline code blocks are good
target for JIT-optimisations.
Now syntax.
('lambda' is an example keyword, just for illustration!!!)
For loop:
codesnippet = FOR_LOOP_FUNCTION(lst,
lambda i:
do whatever with index i
. . .. .
....
: # for closing block explicitely
)
Function:
codesnippet = DEF_FUNCTION(param_names,
lambda pms:
.....
....
return k
:
, defaults = dict1)
Now if we want to provide codesnippet with parameters:
codesnippet = lambda what, filename:
print >> open(filename, "w"), what
:
codesnippet2 = codesnippet % {'filename': 'ex1', }
what = "123"
codesnippet.exec(vars())
Of course, parameter getting/setting needs some formal protocol.
If grammar will not suffer too much, one-line version:
codesn = lambda: print 2*2: + lambda: print 2*2:
codesn.exec()
will give
4
4
on the output.
(Where 'lambda' is an example keyword, just for illustration)
I even think that we can use $ as new type of blocking, needed for this.
It is fundamental addition and needs extraordinary syntax:
codesn = $ print 2*2 $ + $ print 2*2 $
Or maybe some asymettric:
codesnippet = <? print 2*2 ?> + <? print 2*2 ?>
But Guido will never let this fly, even if parser will be happy ;-)
What about
```print 2*2``` + ```print 2*2```
- very near to the sense of what we want to achieve!
Sincerely yours, Roman Suzi
--
rnd@onego.ru =\= My AI powered by Linux RedHat 7.3