Mailing List Archive

#pragmas and method attributes
About a month ago I wrote (but did not publish) a proposal that combined
#pragmas and method attributes. The reason I combined them is that in a
lot of cases method "attributes" are supposed to be available in the
parse tree, before the program begins to run. Here is my rough draft.

----

We've been discussing method attributes for a long time and I think that
it might be worth hashing out in more detail, especially for type
declaration experimentation. I'm proposing a generalization of the
"decl" keyword that hasbeen kicked around in the types-sig. Other
applications include Spark grammar strings, XML pattern-trigger strings,
multiple language doc-strings, IDE "hints", optimization hints,
associated multimedia (down with glass ttys!), IDL definitions, thread
locking declarations, method visibility declarations, ...

Of course some subset of attributes might migrate into Python's "core
language". Decl gives us a place to experiment and get them right before
we do that migration.

Declarations would always be associated with functions, classes or
modules. They would be simple string-keyed values in a dictionary
attached to the function, class or module called __decls__.

The syntax would be

decl {<key> :"value", <key> :"value" }

Key would be a Python name. Value would be any Python string. In the
case of a type declaration it might be:

decl {type:"def(myint: int) returns bar",
french_doc:"Bonjour",
english_doc: "Hello"}
def func( myint ):
return bar()

No string interpolation or other runtime-ish evaluation is done by the
compiler on those strings. Neither the keys nor the values are evaluated
as Python expressions. We could have a feature that would allow values
to be dictionary-ish strings themselves:

decl {type:"def(myint: int) returns bar",
doc : "Bonjour",
languages:{
french: "Hello"}
}

That would presumably be rare (if we allow it at all). Again, there
would be no evaluation or interpolation. The left hand must be a name.
The right must be a

Code which depended on the declaration can do whatever it wants...if it
has some idea of "execution context" and it wants to (e.g.) do
interpolation with things that have percent signs, nobody would stop it.

A decl that applies to a function or class immediately precedes the
funtion or class. A decl that applies to a module precedes all other
statements other than the docstring (which can be before or after).

--
Paul Prescod - ISOGEN Consulting Engineer speaking for himself
"I and my companions suffer from a disease of the heart that can only
be cured with gold", Hernan Cortes
Re: #pragmas and method attributes [ In reply to ]
On Wed, 12 Apr 2000, Paul Prescod wrote:

> About a month ago I wrote (but did not publish) a proposal that combined
> #pragmas and method attributes. The reason I combined them is that in a
> lot of cases method "attributes" are supposed to be available in the
> parse tree, before the program begins to run. Here is my rough draft.

FWIW, I really really like this.

def func(...):
decl {zorb: 'visible', spark: 'some grammar rule'}
pass

Right on!

But maybe even

def func(...):
decl zorb='visible'
decl spark='some grammar rule'
pass

BTW: Why force the value to be a string? Any immutable basic type should
do fine, no??
--
Moshe Zadka <mzadka@geocities.com>.
http://www.oreilly.com/news/prescod_0300.html
http://www.linux.org.il -- we put the penguin in .com
Re: #pragmas and method attributes [ In reply to ]
>>>>> "PP" == Paul Prescod <paul@prescod.net> writes:

PP> About a month ago I wrote (but did not publish) a proposal
PP> that combined #pragmas and method attributes. The reason I
PP> combined them is that in a lot of cases method "attributes"
PP> are supposed to be available in the parse tree, before the
PP> program begins to run. Here is my rough draft.

Very cool. Combine them with Greg Wilson's approach and you've got my
+1 on the idea. I still think it's fine that the func attr dictionary
is writable.

-Barry
Re: #pragmas and method attributes [ In reply to ]
Moshe Zadka wrote:
>
> On Wed, 12 Apr 2000, Paul Prescod wrote:
>
> > About a month ago I wrote (but did not publish) a proposal that combined
> > #pragmas and method attributes. The reason I combined them is that in a
> > lot of cases method "attributes" are supposed to be available in the
> > parse tree, before the program begins to run. Here is my rough draft.
>
> FWIW, I really really like this.
>
> def func(...):
> decl {zorb: 'visible', spark: 'some grammar rule'}
> pass
>
> Right on!
>
> But maybe even
>
> def func(...):
> decl zorb='visible'
> decl spark='some grammar rule'
> pass

Hmm, this is not so far away from simply letting function/method
attribute use the compiled-in names of all locals as basis, e.g.

def func(x):
a = 3

print func.a

func.a would look up 'a' in func.func_code.co_names and return
the corresponding value found in func.func_code.co_consts.

Note that subsequent other assignments to 'a' are not recognized
by this technique, since co_consts and co_names are written
sequentially. For the same reason, writing things like 'a = 2 + 3'
will break this lookup technique.

This would eliminate any need for added keywords and probably
provide the best programming comfort and the attributes
are immutable per se.

We would still have to come up with a way to declare these
attributes for builtin methods and modules...

--
Marc-Andre Lemburg
______________________________________________________________________
Business: http://www.lemburg.com/
Python Pages: http://www.lemburg.com/python/
re: #pragmas and method attributes [ In reply to ]
> > On Wed, 12 Apr 2000, Paul Prescod wrote:
> > About a month ago I wrote (but did not publish) a proposal that combined
> > #pragmas and method attributes. The reason I combined them is that in a
> > lot of cases method "attributes" are supposed to be available in the
> > parse tree, before the program begins to run. Here is my rough draft.

> Moshe Zadka wrote:
> BTW: Why force the value to be a string? Any immutable basic type
> should do fine, no??

If attributes can be objects other than strings, then programmers can
implement hierarchical nesting directly using:

def func(...):
decl {
'zorb' : 'visible',
'spark' : {
'rule' : 'some grammar rule',
'doc' : 'handle quoted expressions'
}
'info' : {
'author' : ('Greg Wilson', 'Allun Smythee'),
'date' : '2000-04-12 14:08:20 EDT'
}
}
pass

instead of:

def func(...):
decl {
'zorb' : 'visible',
'spark-rule' : 'some grammar rule',
'spark-doc' : 'handle quoted expressions'
'info-author' : 'Greg Wilson, Allun Smythee',
'info-date' : '2000-04-12 14:08:20 EDT'
}
pass

In my experience, every system for providing information has eventually
wanted/needed to be hierarchical --- code blocks, HTML, the Windows
registry, you name it. This can be faked up using some convention like
semicolon-separated lists, but processing (and escaping insignificant uses
of separator characters) quickly becomes painful. (Note that if Python
supported multi-dicts, or if something *ML-ish was being used for decl's,
the "author" tag in "info" could be listed twice, instead of requiring
programmers to fall back on char-separated lists.)

Just another random,

Greg