473,396 Members | 1,921 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 473,396 software developers and data experts.

code suite as first class object ?

Useless example first:

def foo(x):
a = suite:
y = 1
b = suite:
y = 2
a() # equivalent effect to local y = 1 above
(a,b)[bool(x)]() # effect of suite b if bool(x), otherwise suite a
vars()[x]() # succeeds if x in ['a', 'b']
return y

Since binding of the result of suite: is in the form of an assignment, it would also be
possible to create a switch with anonymous suites directly, e.g.,

switch = [0]*2
switch[0] = suite: z = 3
switch[1] = suite: z = 4
...
switch[expr]()

or named ones in a chosen directory (of course the name 'switch' has no special status ;-)

shazam={}
shazam['one'] = suite: print 'suite one'
shazam['two'] = suite: print 'suite two'
...
shazam[suitename]() # or use shazam.get variations if desired

or as attributes?

class NS(object): pass
doatt = NS()
...
doatt.one = suite: print 'doatt_one'
doatt.two = suite: print 'doatt_two'
...
doatt.two()
getattr(doatt, attname)()

(IWT bindings in potentially non-local namespaces would generally require closures,
whereas the vars()[suitename]() usage should not, unless vars() is exported (?)).

The above suites are obviously braindead placeholders. I.e., anything legal as a "suite"
should be legal, including nested suites both old and new (i.e., under e.g., "if expr:"
or "s=suite:") Note that suite: does not introduce a new (name) scope, though one suite object
can be invoked from the body of another. I'm not sure how much new stuff this would
introduce into stack-unwinding for exceptions, but it feels like there ought to be a way
to do better than ignoring suite calls as being internal to a frame ...

Implementation would be a parameterless function which uses the *current* local namespace
as its local space. I.e., no separate frame, just a byte code to execute a local function
(pushing return byte code location) and another to return (popping byte code location and
jumping there).

This would get the performance benefit of sequence indexing or dict lookup vs if/elif/else
equivalents without incurring full function call overhead.

I posted something similar before (not the exception thing ;-), but I think this is a little
better. I haven't thought through the closure issues, but there ought to be a way to handle
them, IWT.

Anyone have a real-life example of a long chain of elifs that could be recoded for
realistic comparison?

BTW, maybe "localfun" or "localcode" or "codeblock" would be better words than "suite"?

Of course, this is just HOTTOMH, so fire away and shoot it full of holes ;-)
Maybe if something survives the flames, it could be useful.

Regards,
Bengt Richter
Jul 18 '05 #1
2 2027
I've been thinking about something like this (though not exactly) for the
last few weeks. I did a post ("PEP318 - property as decoration"), where
(amongst other things) I suggested it would be possible to define properties
cleanly using blocks and/or thunks. This idea was based on a discussion from
python-dev in January/February ("Extended Function Syntax"). Blocks and
thunks (as I saw them) seemed powerful. The way I was thinking about them,
it looked like it might be possible to define anonymous functions, and even
anonymous classes. From there, I began toying with a language design that
focused on trying to use only namespaces, bindings, generators, blocks and
thunks (oh, and numbers and strings and lists, etc). I haven't quite worked
out the kinks, but it looks something like Smalltalk with bits and pieces of
Icon, Ruby, and Python thrown in. The idea I was trying to work out was "If
Python were to add thunks (as I pictured them), how could that affect the
language? What sorts of things could I expect to see people trying with
their code?". In other words, "How bad could it get?". Here's an example:

MyClass = object with:
"MyClass = object(thunk), where object returns a class defined using
thunk"

__init__ = method with self, foo:
"__init__ = method(thunk), where method returns a method
defined using thunk"
self._foo = foo

foo = property with:
"property foo's docstring"
fget = with self:
return self._foo
fset = with self, value:
self._foo = value

bar = static, method with:
"bar = static(method(thunk))"
1.upto(10) do with i:
print "upto() yeilds %d which is passed to the
thunk. The thunk is passed to do (do(thunk)) ", \
"where it gets executed, and so you see
this!" % i
So, the language I was working out was not Python as it is now, but as it
might become should something like blocks and thunks be added. I don't find
the language terrible, but I also don't find it to be Python - and that's my
point, I suppose. I like the idea of blocks and thunks and anonymous
functions, classes, etc., but I don't like the idea that there could be more
than one way to define a function, method, or class.

If you read the python-dev discussions, the ideas presented for thunks
appear to enable macro programming as well. For instance, using Guido's
definition of thunks he was able to construct a switch statement, but not
like yours, like this:

[Guido]
switch(expr):
case(val1):
block1
case(val2):
block2
default:
block3

To which he added:
This actually makes me worry -- I didn't plan thunks to be the answer
to all problems. A new idea that could cause a paradigm landslide is
not necessarily right.


A statement I'd have to say I agree with.


Jul 18 '05 #2
The code in the last post looked a bit messed up on my newsreader, so here
it is again:

MyClass = object with:
"""MyClass = object(thunk), where object
returns a class defined using thunk
"""
__init__ = method with self, foo:
"""__init__ = method(thunk), where method
returns a method defined using thunk
"""
self._foo = foo

foo = property with:
"property foo's docstring"
fget = with self:
return self._foo
fset = with self, value:
self._foo = value

bar = static, method with:
"bar = static(method(thunk))"
1.upto(10) do with i:
print "upto() yeilds %d which is passed to ", \
"the thunk. The thunk is passed to do ", \
"(do(thunk)), where it gets executed, ", \
"and so you see this!" % i
Jul 18 '05 #3

This thread has been closed and replies have been disabled. Please start a new discussion.

Similar topics

699
by: mike420 | last post by:
I think everyone who used Python will agree that its syntax is the best thing going for it. It is very readable and easy for everyone to learn. But, Python does not a have very good macro...
0
by: Chris McKeever | last post by:
I am trying to modify the Mailman Python code to stop mapping MIME-types and use the extension of the attachment instead. I am pretty much clueless as to what I need to do here, but I think I have...
2
by: Sylvain Thenault | last post by:
Hi there ! I've noticed the following problem with python >= 2.3 (actually 2.3.4 and 2.4): syt@musca:test$ python Python 2.3.4 (#2, Sep 24 2004, 08:39:09) on linux2 Type "help", "copyright",...
31
by: Brian Sabbey | last post by:
Here is a pre-PEP for what I call "suite-based keyword arguments". The mechanism described here is intended to act as a complement to thunks. Please let me know what you think. Suite-Based...
0
by: Bengt Richter | last post by:
We have where syntax in combination with suite expression syntax (bear with me, I think a good synergy will emerge ;-) ...
15
by: Ralf W. Grosse-Kunstleve | last post by:
****************************************************************************** This posting is also available in HTML format: http://cci.lbl.gov/~rwgk/python/adopt_init_args_2005_07_02.html...
4
by: William | last post by:
After much frustration I was able to update my data store via code only. Using the data adapter was the only way I was able to set up all the objects written in my code. Basically, I cheated by...
17
by: CBFalconer | last post by:
David Brown wrote: .... snip ... The problem with the gcc test suite is that it is geared to the gcc 'standard', rather than the ISO standard. A test suite should be open-source, and there...
4
by: David | last post by:
Hi list. Do test-driven development or behaviour-driven development advocate how to do higher-level testing than unit testing? types of testing: unit integration system
0
by: Charles Arthur | last post by:
How do i turn on java script on a villaon, callus and itel keypad mobile phone
1
by: nemocccc | last post by:
hello, everyone, I want to develop a software for my android phone for daily needs, any suggestions?
1
by: Sonnysonu | last post by:
This is the data of csv file 1 2 3 1 2 3 1 2 3 1 2 3 2 3 2 3 3 the lengths should be different i have to store the data by column-wise with in the specific length. suppose the i have to...
0
marktang
by: marktang | last post by:
ONU (Optical Network Unit) is one of the key components for providing high-speed Internet services. Its primary function is to act as an endpoint device located at the user's premises. However,...
0
by: Hystou | last post by:
Most computers default to English, but sometimes we require a different language, especially when relocating. Forgot to request a specific language before your computer shipped? No problem! You can...
0
Oralloy
by: Oralloy | last post by:
Hello folks, I am unable to find appropriate documentation on the type promotion of bit-fields when using the generalised comparison operator "<=>". The problem is that using the GNU compilers,...
0
by: Hystou | last post by:
Overview: Windows 11 and 10 have less user interface control over operating system update behaviour than previous versions of Windows. In Windows 11 and 10, there is no way to turn off the Windows...
0
tracyyun
by: tracyyun | last post by:
Dear forum friends, With the development of smart home technology, a variety of wireless communication protocols have appeared on the market, such as Zigbee, Z-Wave, Wi-Fi, Bluetooth, etc. Each...
0
agi2029
by: agi2029 | last post by:
Let's talk about the concept of autonomous AI software engineers and no-code agents. These AIs are designed to manage the entire lifecycle of a software development project—planning, coding, testing,...

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.