Meta programming question

B

Bruce Dickey

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
 
B

Ben Finney

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.
 
B

Bruce Dickey

Ben said:
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
 
T

Terry Reedy

Bruce Dickey said:
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
 
A

Alex Martelli

Bruce said:
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
 
J

Jeff Epler

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 thatwill 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/python-dev/2002-October/029761.html

Jeff
 
D

David Abrahams

Bruce Dickey said:
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.
 
B

Bruce Dickey

David Abrahams said:
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.

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

Bruce
 
B

Bruce Dickey

Alex Martelli said:
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
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

No members online now.

Forum statistics

Threads
473,744
Messages
2,569,483
Members
44,903
Latest member
orderPeak8CBDGummies

Latest Threads

Top