473,626 Members | 3,247 Online
Bytes | Software Development & Data Engineering Community
+ Post

Home Posts Topics Members FAQ

code blocks in Python

Hi,

I know people have talked about it before, but I am still really
confused from reading the old messages.

When I talk about code blocks, I am not talking about Lisp/Ruby/Perl,
so I am not making comparisons.

If you have a file, in Python you could do:

code = compile(code_st ring, 'FileName.py', 'exec')

and obtain a code object, which later on you can execute. That's the
kind of code block I have in mind. That is, no parameters, no
specification of scopes, no nothing else. Just a plain block of code
lines compiled into a code object.

It's not too much work to write a code string like:

my_codeblock = '''
x = x + 1
y = y + 2
'''

and then use compile() to turn it into a code object. But it seems to
me that Python could perfectly allow this syntax directly into the
language, something like:

codeblock my_codeblock:
x = x + 1
y = y + 2

so that users won't need to explicitly call the compile() function.
The advantage is that the codeblock is compiled at compile time, not
at run time, even if the codeblock is defined inside something else (a
class, a function, etc.)

Is there something obvious that I am missing here? Why isn't codeblock
part of the language? It seems to be an important building element for
meta-programming. Not only that, it seems to be much more fundamental
than functions. So, why isn't it part of Python? Any intrinsic
difficulties?

regards,

Hung Jung
Jul 18 '05 #1
20 5922

"Hung Jung Lu" <hu********@yah oo.com> wrote in message
news:8e******** *************** **@posting.goog le.com...
Hi,

I know people have talked about it before, but I am still really
confused from reading the old messages.

When I talk about code blocks, I am not talking about Lisp/Ruby/Perl,
so I am not making comparisons.

If you have a file, in Python you could do:

code = compile(code_st ring, 'FileName.py', 'exec')

and obtain a code object, which later on you can execute. That's the
kind of code block I have in mind. That is, no parameters, no
specification of scopes, no nothing else. Just a plain block of code
lines compiled into a code object.

It's not too much work to write a code string like:

my_codeblock = '''
x = x + 1
y = y + 2
'''

and then use compile() to turn it into a code object. But it seems to
me that Python could perfectly allow this syntax directly into the
language, something like:

codeblock my_codeblock:
x = x + 1
y = y + 2

so that users won't need to explicitly call the compile() function.
The advantage is that the codeblock is compiled at compile time, not
at run time, even if the codeblock is defined inside something else (a
class, a function, etc.)

Is there something obvious that I am missing here? Why isn't codeblock
part of the language? It seems to be an important building element for
meta-programming. Not only that, it seems to be much more fundamental
than functions. So, why isn't it part of Python? Any intrinsic
difficulties?
Just some random observations. First, I'm not at all clear on
what you want it to do. Some examples would be helpful. There
are entirely too many different ways I could interpret your reference
to metaprogramming for me to understand the usage you have in
mind.

If you want it as a replacement for the badly crippled lambda statement,
then I think there's quite a bit of interest. What's holding that up is
syntax, and an appropriate syntax isn't entirely obvious.

If you want it to do something else, like act like it was actually
compiled where it will be invoked, with identifier resolution
and all, then that's a completely different subject.

John Roth


regards,

Hung Jung

Jul 18 '05 #2
"John Roth" <ne********@jhr othjr.com> wrote in message news:<vr******* *****@news.supe rnews.com>...
Just some random observations. First, I'm not at all clear on
what you want it to do. Some examples would be helpful. There
are entirely too many different ways I could interpret your reference
to metaprogramming for me to understand the usage you have in
mind.
codeblock A:
x = 1
y = 2

codeblock B:
z = x + y
print z

exec A
exec B

This will be equivalent to writing the program:

x = 1
y = 2
z = x + y
print z
If you want it as a replacement for the badly crippled lambda statement,
then I think there's quite a bit of interest. What's holding that up is
syntax, and an appropriate syntax isn't entirely obvious.


Lambda is a totally different thing. Lambda is an anonymous
*function*. I emphasize *function* because functions take parameters
and return values. Code blocks are, well, blocks of code. No
parameters, no nothing else. Simple and plain blocks of code. Once you
have codeblocks, you wire them anyway you want. Think of them as
macros in C++, only that in Python you can wire them dynamically
during runtime. Python already does that. It's just that you need the
compile() function, your code string is not compiled until the
compile() function is executed, and the resulting object type is
"Code" (which is a fine name, even if the new keyword were
"codeblock" .) Have you tried the compile() function?

regards,

Hung Jung
Jul 18 '05 #3
On 22 Nov 2003 19:13:19 -0800, hu********@yaho o.com (Hung Jung Lu) wrote:
"John Roth" <ne********@jhr othjr.com> wrote in message news:<vr******* *****@news.supe rnews.com>...
Just some random observations. First, I'm not at all clear on
what you want it to do. Some examples would be helpful. There
are entirely too many different ways I could interpret your reference
to metaprogramming for me to understand the usage you have in
mind.


codeblock A:
x = 1
y = 2

codeblock B:
z = x + y
print z

exec A
exec B

This will be equivalent to writing the program:

x = 1
y = 2
z = x + y
print z
If you want it as a replacement for the badly crippled lambda statement,
then I think there's quite a bit of interest. What's holding that up is
syntax, and an appropriate syntax isn't entirely obvious.


Lambda is a totally different thing. Lambda is an anonymous
*function*. I emphasize *function* because functions take parameters
and return values. Code blocks are, well, blocks of code. No
parameters, no nothing else. Simple and plain blocks of code. Once you
have codeblocks, you wire them anyway you want. Think of them as
macros in C++, only that in Python you can wire them dynamically
during runtime. Python already does that. It's just that you need the
compile() function, your code string is not compiled until the
compile() function is executed, and the resulting object type is
"Code" (which is a fine name, even if the new keyword were
"codeblock". ) Have you tried the compile() function?

I note that:
compile('a=123' ,'','single') <code object ? at 0090D5A0, file "", line 1> import dis
dis.dis(compile ('a=123','','si ngle')) 1 0 LOAD_CONST 0 (123)
3 STORE_NAME 0 (a)
6 LOAD_CONST 1 (None)
9 RETURN_VALUE def foo(): a=123 ... dis.dis(foo)

1 0 LOAD_CONST 1 (123)
3 STORE_FAST 0 (a)
6 LOAD_CONST 0 (None)
9 RETURN_VALUE

So which code will you want when the compiler compiles an in-context block?
Will you want it to be a first-class object? Should you be able to
pass the code-block name to an arbitrary function or bind it globally, like you can
with compile's output? Are you willing to forego the optimized local access? Or want to
exec it later from anywhere? What should happen? You could call for magic creation of special
closures so that an exported code block would still refer back to the local name space
(which BTW could be interesting for generators to yield, to give external access to their
internal state). And what should assigning a function local codeblock to a global mean?
Maybe creating a one-shot generator where the codeblock could be used to preset state for
a single ordinary function call that re-uses previous state. Or should it just die like a
weak reference?

IOW, there are some things to think about, because the semantics could get interesting ;-)
It would be nice to have efficient local blocks, so you could write exec(locals()[switcharg])
or perhaps some more efficient builtin switch/case thing using them. The efficiency of
a large jump table (dict or tuple/list based) vs a chain of elifs is a good motivation for some uses,
but I wonder how common/important they are.

Regards,
Bengt Richter
Jul 18 '05 #4
bo**@oz.net (Bengt Richter) wrote in message news:<bp******* ***@216.39.172. 122>...
>>> compile('a=123' ,'','single') <code object ? at 0090D5A0, file "", line 1> >>> import dis
>>> dis.dis(compile ('a=123','','si ngle')) 1 0 LOAD_CONST 0 (123)
3 STORE_NAME 0 (a)
6 LOAD_CONST 1 (None)
9 RETURN_VALUE >>> def foo(): a=123 ... >>> dis.dis(foo)
1 0 LOAD_CONST 1 (123)
3 STORE_FAST 0 (a)
6 LOAD_CONST 0 (None)
9 RETURN_VALUE

So which code will you want when the compiler compiles an in-context block?

Will you want it to be a first-class object?
Yes, absolutely.
with compile's output? Are you willing to forego the optimized local access?
I know there is a performance issue, since in normal, local, non-named
codeblocks, local variables are retrieved by indices instead of names.
There is a question as how to hook up named codeblocks efficiently in
the scope of the non-named codeblocks. But one is gotta start
somewhere. Optimization comes later.
You could call for magic creation of special
closures so that an exported code block would still refer back to the local name space
(which BTW could be interesting for generators to yield, to give external access to their
internal state).
Again, something simple to start with. Closures comes later, if at all
necessary. I am not sure whether closure-like codeblocks are
fundamental enough, since they could be emulated by other means. E.g.

class generic: pass

codeblock A:
print c.x
c.x = 3

c1 = generic()
c2 = generic()
c1.x = 4
c1.code = A
c2.x = 5
c2.code = A

c = c1
exec c.code

c = c2
exec c.code
And what should assigning a function local codeblock to a global mean?
Maybe creating a one-shot generator where the codeblock could be used to preset state for
a single ordinary function call that re-uses previous state. Or should it just die like a
weak reference?
I don't know exactly what you mean by "function local codeblock". If
you refer to the function's code, currently accessible as f.func_code,
I've tried that, but it seems to bind things to the locals dictionary
of the function itself. Which is no good. E.g.:

def f():
x = 1

def g(x):
exec f.func_code
print x

g(2)
x = 3
exec f.func_code in globals()
print x

will print 2 and 3. I tried using exec...in... with various
namespaces, but the same thing. That is, the func_code remembers its
own local scope and refuses access/modify other local scopes. This is
very different from:

code = compile('x=1\n' , '<string>', 'exec')

def g(x):
exec code
print x

g(2)
x = 3
exec code
print x

which will print 1 and 1. That is, smart enough to take x from the
respective local and global dictionaries.

----------

If by "function local codeblock" you mean codeblocks define inside the
scope of a function, I really don't think they should receive any
special treatment for optimization, since a codeblock should be an
object that can be passed around, and should be independent of where
they are defined. It's like if you have the number 3, which is an
object, you can pass it around to anywhere, regardless where it was
first defined. No complications from nested-scopes, closures, etc.
Functions get complicated exactly because they carry all these extra
baggages.

----------

Well, all these are the kinds of things to think about. But named
codeblocks have well-defined meaning on their own right, independent
of implementation details. Theoretically, codeblocks don't know
whether the names inside them are local or global. It's a late-binding
object.
but I wonder how common/important they are.


I can't say in the case you have described. However, in general
codeblocks are extremely useful, right now they are not used as
widely, I think simply because the codestrings are not compiled and
checked at compile time: you need one extra step invoking the
compile() function. Named codeblocks will solve a barrage of nuisances
in today's programming world. They helps in (a) code re-use, (b) code
modularization. By (a) I mean that the same codeblock could be used in
a lot of places. By (b) I mean that you can isolate the effects of
codeblocks, and do code replacements a la AOP (Aspect-Oriented
Programming.)

It's just weird that Python has the compile() function, but has no
named codeblocks. I really can't understand why.

regards,

Hung Jung
Jul 18 '05 #5
hu********@yaho o.com (Hung Jung Lu) wrote in message >
codeblock A:
x = 1
y = 2

codeblock B:
z = x + y
print z

exec A
exec B

This will be equivalent to writing the program:

x = 1
y = 2
z = x + y
print z


It just occurred to me a better syntax:

def A:
x = 1
y = 2

def B:
z = x + y
print z

No new keyword needed, beside, it uses the same keyword as function
definition, so the purpose of the code block is clear: it's something
that you will invoke.

A proper name for this type of meta-programming is "Linear
Meta-programming". Just like in algebra you have linear algebra and
non-linear algebra, in meta-programming you have all sorts of
non-linear ways of taking code pieces and turn them into other code
pieces, these include e.g.: (a) compiling: turning a character string
into a binary code string, (b) encryption, (c) macro/token
substitution, (d) tweaking with the internals like meta-classes, etc.
Linear meta-programming is much more modest and well-organized. It's
like sequencing DNA bands. Each statement is like a DNA nucleotide,
each codeblock is like a DNA band. You just want to sequence the
codeblocks properly, and at times replace one block with some other
block, or insert additional blocks.

I still have yet to hear any difficulties/complaints.

I refuse to believe that Python developers have NEVER considered this
extension to the language. So, what's the problem?

Hung Jung
Jul 18 '05 #6
hu********@yaho o.com (Hung Jung Lu) writes:
I still have yet to hear any difficulties/complaints.


OK: 1. the semantics of your proposed extension remain somewhat nebulous
2. the extension appears pretty useless (AFAICT, given 1.)
3. you still haven't provided an example that would demonstrate otherwise
(i.e. something interesting that could be more easily written with
python + your extension than with python now).

'as

Jul 18 '05 #7
On Sun, Nov 23, 2003 at 04:34:39PM -0800, Hung Jung Lu wrote:
I still have yet to hear any difficulties/complaints.
Suppose I write this:

def C:
x = x + 1

def a():
x = 2
exec C
print x

def b():
exec C

how will the implementation of "code blocks" deal with the fact that in
a, x is a fastlocal with a particular index, but in b there's no local
x? (In cpython, "bare exec" has a significant negative impact of the
performance of a function, because all variable accesses happen though
LOAD_NAME instead of LOAD_GLOBAL / LOAD_FAST. Any new block-oriented
feature should be designed to avoid this problem)
I refuse to believe that Python developers have NEVER considered this
extension to the language. So, what's the problem?


Extensions along these lines have often been discussed. Consider reading
the archives. "Macros", "code blocks", "with blocks", and "closures" are
all terms you might consider using if you are searching for past messages.

Jeff

Jul 18 '05 #8
Jeff Epler <je****@unpytho nic.net> wrote in message news:<ma******* *************** *************** @python.org>...
def C:
x = x + 1

def a():
x = 2
exec C
print x

def b():
exec C

how will the implementation of "code blocks" deal with the fact that in
a, x is a fastlocal with a particular index, but in b there's no local
x? (In cpython, "bare exec" has a significant negative impact of the
performance of a function, because all variable accesses happen though
LOAD_NAME instead of LOAD_GLOBAL / LOAD_FAST. Any new block-oriented
feature should be designed to avoid this problem)


I agree that performance could be made better. But that's an issue for
later. If codeblock becomes a fundamental building block of a language
(say, Python 3.0), I am sure there are tricks beyond the current
global/local/builtin namespaces mechanisms
(LOAD_NAME/LOAD_FAST/LOAD_GLOBAL). But performance is an issue for
later. I would like to remind people that the compile() function
exists and works in Python. For whatever discussion on this subject,
the first thing to do is to replace the codeblock definition with the
compile function() and analyze from there on. In this case,
C=compile('x=x+ 1\n', '<string>', 'exec').

regards,

Hung Jung
Jul 18 '05 #9

Jeff> how will the implementation of "code blocks" deal with the fact
Jeff> that in a, x is a fastlocal with a particular index, but in b
Jeff> there's no local x?

hjl> I agree that performance could be made better. But that's an issue
hjl> for later....

You ignored Jeff's primary questions. What are the semantics of a named
code block such as you propose? What if it's executed in a context which
doesn't contain the variables it uses? What expressive power does it give
to Python that Python doesn't currently have?

Skip
Jul 18 '05 #10

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

Similar topics

2
2045
by: Bengt Richter | last post by:
Useless example first: def foo(x): a = suite: y = 1 b = suite: y = 2 a() # equivalent effect to local y = 1 above (a,b)() # effect of suite b if bool(x), otherwise suite a vars()() # succeeds if x in
699
33836
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 capabilities, unfortunately. I'd like to know if it may be possible to add a powerful macro system to Python, while keeping its amazing syntax, and if it could be possible to add Pythonistic syntax to Lisp or Scheme, while keeping all of the...
2
2107
by: dave | last post by:
downaloded from ftp.python.org and failed to unzip the files: x Python-2.3.1/Mac/OSXResources/app/Resources/English.lproj/Documentation/ide/index.html, 9909 bytes, 20 tape blocks x Python-2.3.1/Mac/OSXResources/app/Resources/English.lproj/Documentation/ide/IDE.gif, 10249 bytes, 21 tape blocks tar: directory checksum error
3
1908
by: Dave Benjamin | last post by:
Hey all, I was just reflecting upon the old code block debate, and the thought occurred to me that Python really does have a form of code blocks in its implementation of generators. It was a pretty natural conclusion, and I'm not sure why I didn't think of it before. For instance, compare Python's generator-style iteration versus Ruby's codeblock-style iteration: Python: for item in seq:
20
2002
by: Doug Holton | last post by:
Is there any metaclass trick or something similar to allow anonymous code blocks? I'd like to be able to let users do something like this fictitious example: b = Button() b.OnClick =: print "you clicked me" But that would require adding a special "=:" operator to bind a code block to a function.
0
1583
by: lazaridis_com | last post by:
Colored Code Blocks Pudge generated documentation can contain colored code blocks, using the rst directive "..code-block:: Python" (an other languages supported by SilverCity). http://pudge.lesscode.org/trac/ticket/21 http://pudge.lesscode.org/trac/changeset/126
13
5107
by: bayer.justin | last post by:
Hi, I am trying to communicate with a subprocess via the subprocess module. Consider the following example: <subprocess.Popen object at 0x729f0> Here hey is immediately print to stdout of my interpreter, I did not type in the "hey". But I want to read from the output into a string, so I do
4
1405
by: DeveloperX | last post by:
I am trying to figure out how to rewrite the following chunk of code in Python: C source typedef struct PF { int flags; long user; char*filename;
24
2513
by: David | last post by:
Hi list. What strategies do you use to ensure correctness of new code? Specifically, if you've just written 100 new lines of Python code, then: 1) How do you test the new code? 2) How do you ensure that the code will work correctly in the future? Short version:
0
8203
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 effortlessly switch the default language on Windows 10 without reinstalling. I'll walk you through it. First, let's disable language synchronization. With a Microsoft account, language settings sync across devices. To prevent any complications,...
0
8711
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, it seems that the internal comparison operator "<=>" tries to promote arguments from unsigned to signed. This is as boiled down as I can make it. Here is my compilation command: g++-12 -std=c++20 -Wnarrowing bit_field.cpp Here is the code in...
0
8642
jinu1996
by: jinu1996 | last post by:
In today's digital age, having a compelling online presence is paramount for businesses aiming to thrive in a competitive landscape. At the heart of this digital strategy lies an intricately woven tapestry of website design and digital marketing. It's not merely about having a website; it's about crafting an immersive digital experience that captivates audiences and drives business growth. The Art of Business Website Design Your website is...
0
8512
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 protocol has its own unique characteristics and advantages, but as a user who is planning to build a smart home system, I am a bit confused by the choice of these technologies. I'm particularly interested in Zigbee because I've heard it does some...
0
7203
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, and deployment—without human intervention. Imagine an AI that can take a project description, break it down, write the code, debug it, and then launch it, all on its own.... Now, this would greatly impact the work of software developers. The idea...
1
6125
isladogs
by: isladogs | last post by:
The next Access Europe User Group meeting will be on Wednesday 1 May 2024 starting at 18:00 UK time (6PM UTC+1) and finishing by 19:30 (7.30PM). In this session, we are pleased to welcome a new presenter, Adolph Dupré who will be discussing some powerful techniques for using class modules. He will explain when you may want to use classes instead of User Defined Types (UDT). For example, to manage the data in unbound forms. Adolph will...
0
5576
by: conductexam | last post by:
I have .net C# application in which I am extracting data from word file and save it in database particularly. To store word all data as it is I am converting the whole word file firstly in HTML and then checking html paragraph one by one. At the time of converting from word file to html my equations which are in the word document file was convert into image. Globals.ThisAddIn.Application.ActiveDocument.Select();...
0
4206
by: adsilva | last post by:
A Windows Forms form does not have the event Unload, like VB6. What one acts like?
1
2630
by: 6302768590 | last post by:
Hai team i want code for transfer the data from one system to another through IP address by using C# our system has to for every 5mins then we have to update the data what the data is updated we have to send another system

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.