Proposal for adding symbols within Python

Steven D'Aprano steve at REMOVETHIScyber.com.au
Sun Nov 13 16:41:53 CET 2005


On Mon, 14 Nov 2005 00:48:46 +1100, Ben Finney wrote:

> Steven D'Aprano <steve at removethiscyber.com.au> wrote:
>> On Sun, 13 Nov 2005 10:11:04 +0100, Pierre Barbier de Reuille wrote:
>> > The problem, IMHO, is that way you need to declare "symbols"
>> > beforehands, that's what I was trying to avoid by requiring a new
>> > syntax.
>> 
>> If you don't declare your symbols, how will you get the ones that
>> you want?
>> [...]
>> Are you suggesting that the Python language designers should somehow
>> predict every possible symbol that anyone in the world might ever
>> need, and build them into the language as predefined things?
> 
> I believe Pierre is looking for a syntax that will save him from
> assigning values to names; that Python will simply assign arbitrary
> unique values for these special names. My understanding of the
> intended use is that their only purpose is to compare differently to
> other objects of the same type, so the actual values don't matter.

Unless I've misunderstood something, it would be easy to modify the recipe
given here to do something like that:

http://aspn.activestate.com/ASPN/Cookbook/Python/Recipe/413486

The example code does this:

Days = Enum('Mo', 'Tu', 'We', 'Th', 'Fr', 'Sa', 'Su')
print Days.Mo, Days.Tu
# etc.


> What I still don't understand is why this justifies additional syntax
> baggage in the language, rather than an explicit assignment earlier in
> the code.

The only advantage would be if you want to do something like this:

MO, TU, WE, TH, FR, SA, SU = Symbols()

and have it magically work. I can see the advantage of that, and you don't
even need new syntax, just a magic function that somehow knows how many
names are on the left hand side of the assignment.

This is a poor substitute:

MO, TU, WE, TH, FR, SA, SU = range(7)

Firstly, if you change the number of symbol names, you have to manually
adjust the argument to range. Secondly, your symbols are actually ints,
and so will compare the same way ints compare.


I don't know enough about the Python internals to tell: is there any
feasible way for there to be a magic function like Symbol above that knew
how many names were waiting for it to be supplied? If there is no way to
do this from Python itself, it is possible to patch the compiler to do so?



-- 
Steven.




More information about the Python-list mailing list