python-2.1 function attributes
Duncan Booth
duncan.booth at lineone.net
Thu Jan 25 06:18:28 EST 2001
Paul Prescod <paulp at ActiveState.com> wrote in
<mailman.980396369.12407.python-list at python.org>:
>Mike Romberg wrote:
>>
>> ...
>>
>> The second syntax proposed in pep 232 does not strike me as
>> elegant. But, it does seem to be consistant with doc strings in that
>> they are defined (but not referenced) first thing in the function
>> definition:
>>
>> def spam():
>> """The doc string."""
>> { 'attribute1' : 'val1', 'attribute2' : val2 }
>> pass
>
>I proposed a similar syntax to Guido a couple of years ago and he
>pointed out that it is unclear whether "val2" should be evaluated in the
>runtime context of spam() or in the global context. The former is what
>the syntax suggests but the latter is the only way to have the feature
>work properly.
>
> Paul Prescod
>
How about something along the following lines?
__attributes__= { 'attribute1' : 'val1', 'attribute2' : val2 }
def spam():
"""The doc string."""
pass
The idea being that if __attributes__ is set when defining a function, the
value is used to initialise the function attributes and then deleted.
This has the advantage of keeping the attributes close to the top of the
function, but making the context in which they are evaluated obvious.
More information about the Python-list
mailing list