473,386 Members | 1,793 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,386 software developers and data experts.

Meta programming question

Hi,

I've read a number of the meta progamming articles. I have not found
what I am looking for. I want to override assignments to variables which
are in the module namespace (not object members -- no classes involved).
Can this be done?

Thanks,

Bruce

Jul 18 '05 #1
8 1582
On Sat, 27 Sep 2003 19:18:15 -0600, Bruce Dickey wrote:
I've read a number of the meta progamming articles. I have not found
what I am looking for. I want to override assignments to variables
which are in the module namespace (not object members -- no classes
involved). Can this be done?


AFAIK, no. If it were possible, it would be very confusing.

What is it you're trying to achieve? Perhaps a better alternative can
be suggested that doesn't break expected behaviour.

--
\ "Cross country skiing is great if you live in a small country." |
`\ -- Steven Wright |
_o__) |
Ben Finney <http://bignose.squidly.org/>
Jul 18 '05 #2
Ben Finney wrote:
On Sat, 27 Sep 2003 19:18:15 -0600, Bruce Dickey wrote:
I've read a number of the meta progamming articles. I have not found
what I am looking for. I want to override assignments to variables
which are in the module namespace (not object members -- no classes
involved). Can this be done?

AFAIK, no. If it were possible, it would be very confusing.

What is it you're trying to achieve? Perhaps a better alternative can
be suggested that doesn't break expected behaviour.

I'm investigating the use of Python as a grammar language. I'm trying to
achive minimal required syntax/verbage.

Thanks,

Bruce

Jul 18 '05 #3
"Bruce Dickey" <ce****@excite.com> wrote in message
news:vn************@corp.supernews.com...
I've read a number of the meta progamming articles. I have not found
what I am looking for. I want to override assignments to variables which are in the module namespace (not object members -- no classes involved). Can this be done?


Yes -- by rewriting the appropriate section of the source code. Since
it is clearly written, open source, and freely recompilable, this is
doable by a competant and motivated C programmer. But given how
intertwined current assignment semantics are with the rest of the
language, I have trouble imaging an alternative that would make sense.

Terry J. Reedy
Jul 18 '05 #4
Bruce Dickey wrote:
I've read a number of the meta progamming articles. I have not found
what I am looking for. I want to override assignments to variables which
are in the module namespace (not object members -- no classes involved).
Can this be done?


No (not without, as somebody already suggested, rewriting substantial
portions of Python's C-level implementation). A module-level statement

<name> = <expression>

evaluates the expression, and sets the name to refer to its value -- no
ifs, no buts, no overriding, and nothing to do with metaprogramming.
Alex

Jul 18 '05 #5
The changes are not large, if I understand what the OP is asking for.
However, they have a performance impact on the "common case".

In Python, a dict subclass's __setitem__ is never invoked when
evaluating bytecode. With a change to just a few opcodes (STORE_GLOBAL,
STORE_NAME), this can be rectified, so that
exec "x = 3" in d

will call d.__setitem__("x", 3), and you can do whatever you need to do
on "assignment".

The patch was never finished, and never added to the sf patch manager.
You can visit the python-dev thread at
http://mail.python.org/pipermail/pyt...er/029761.html

Jeff

Jul 18 '05 #6
Bruce Dickey <ce****@excite.com> writes:
Ben Finney wrote:
On Sat, 27 Sep 2003 19:18:15 -0600, Bruce Dickey wrote:
I've read a number of the meta progamming articles. I have not found
what I am looking for. I want to override assignments to variables
which are in the module namespace (not object members -- no classes
involved). Can this be done?

AFAIK, no. If it were possible, it would be very confusing.
What is it you're trying to achieve? Perhaps a better alternative
can
be suggested that doesn't break expected behaviour.

I'm investigating the use of Python as a grammar language. I'm trying
to achive minimal required syntax/verbage.


Cool! You might take a look at http://spirit.sf.net for some ideas.
Python's a great language for metaprogramming because of its rich
syntax. If you were willing to discourage left-recursion you might go
with something like:

expression = term._('+').expression | term._('-').expression

On the other hand, when I had to do something like this I built my own
"python-like" syntax for the grammar rules and left the semantic
actions in pure Python, then ran the files through a simple
preprocessor. It's just too ugly to write grammar rules in pure
python without overloaded whitespace.

--
Dave Abrahams
Boost Consulting
www.boost-consulting.com
Jul 18 '05 #7

"David Abrahams" <da**@boost-consulting.com> wrote in message
news:uf***********@boost-consulting.com...
Bruce Dickey <ce****@excite.com> writes:
Ben Finney wrote:
On Sat, 27 Sep 2003 19:18:15 -0600, Bruce Dickey wrote:

I've read a number of the meta progamming articles. I have not found
what I am looking for. I want to override assignments to variables
which are in the module namespace (not object members -- no classes
involved). Can this be done?
AFAIK, no. If it were possible, it would be very confusing.
What is it you're trying to achieve? Perhaps a better alternative
can
be suggested that doesn't break expected behaviour.

I'm investigating the use of Python as a grammar language. I'm trying
to achive minimal required syntax/verbage.


Cool! You might take a look at http://spirit.sf.net for some ideas.
Python's a great language for metaprogramming because of its rich
syntax. If you were willing to discourage left-recursion you might go
with something like:

expression = term._('+').expression | term._('-').expression

On the other hand, when I had to do something like this I built my own
"python-like" syntax for the grammar rules and left the semantic
actions in pure Python, then ran the files through a simple
preprocessor. It's just too ugly to write grammar rules in pure
python without overloaded whitespace.

--
Dave Abrahams
Boost Consulting
www.boost-consulting.com


I'll look into it, thanks. But I had essentially come to the same
conclusion.

Bruce
Jul 18 '05 #8

"Alex Martelli" <al***@aleax.it> wrote in message
news:zn**********************@news1.tin.it...
Bruce Dickey wrote:
I've read a number of the meta progamming articles. I have not found
what I am looking for. I want to override assignments to variables which
are in the module namespace (not object members -- no classes involved).
Can this be done?


No (not without, as somebody already suggested, rewriting substantial
portions of Python's C-level implementation). A module-level statement

<name> = <expression>

evaluates the expression, and sets the name to refer to its value -- no
ifs, no buts, no overriding, and nothing to do with metaprogramming.
Alex


I think the path of least resistance is to preprocess a (non-Python)
grammar language. My OP was a shot in the dark.

Thanks,

Bruce
Jul 18 '05 #9

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

Similar topics

0
by: taylorcarr | last post by:
A Canon printer is a smart device known for being advanced, efficient, and reliable. It is designed for home, office, and hybrid workspace use and can also be used for a variety of purposes. However,...
0
by: Charles Arthur | last post by:
How do i turn on java script on a villaon, callus and itel keypad mobile phone
0
by: ryjfgjl | last post by:
If we have dozens or hundreds of excel to import into the database, if we use the excel import function provided by database editors such as navicat, it will be extremely tedious and time-consuming...
0
by: emmanuelkatto | last post by:
Hi All, I am Emmanuel katto from Uganda. I want to ask what challenges you've faced while migrating a website to cloud. Please let me know. Thanks! Emmanuel
0
BarryA
by: BarryA | last post by:
What are the essential steps and strategies outlined in the Data Structures and Algorithms (DSA) roadmap for aspiring data scientists? How can individuals effectively utilize this roadmap to progress...
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
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
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...

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.