Proposal for adding symbols within Python

Pierre Barbier de Reuille pierre.barbier at cirad.fr
Sun Nov 13 10:41:37 EST 2005


Steven D'Aprano a écrit :
> On Sun, 13 Nov 2005 12:33:48 +0100, Pierre Barbier de Reuille wrote:
> 
> 
>>Steven D'Aprano a écrit :
>>[...]
> 
> 
> If you want to be technical, Python doesn't have variables. It has names
> and objects.
> 
> If I want a name x to be bound to an object 1, I have to define it
> (actually bind the name to the object):
> 
> x = 1
> 
> If I want a symbol $x$ (horrible syntax!!!) with a value 1, why shouldn't
> I define it using:
> 
> $x$ = 1
> 
> instead of expecting Python to somehow magically know that I wanted it?
> What if somebody else wanted the symbol $x$ to have the value 2 instead?
> 

Well, as stated, I don't care about the actual value of symbols. They
*are* values. A trivial implementation of symbols are strings :

$x$ <=> "x"

However, that won't fit because of the scope, because it would be great
to use "is" instead of "==" (even if not necessary), and as said Mike,
you might want something else than a string. That's why `x` would be a
good wawy to write that.

> 
> 
>>>[snip]
> 
> 
> I've read the discussion, and I am no wiser.
> 
> You haven't explained why enums are not suitable to be used for symbols.
> You gave two "problems", one of which was "easy to fix", as you said
> yourself, and the other reason was that you don't want to define enums as
> symbols. 
> 
> If you don't want to define something manually, that can only mean that
> you expect them to be predefined. Or am I misunderstanding something?
> 

Well, I suspect Python will know them, exactly as it know "without
defining it" that "foo" is the string with chars f, o, o, that 3 is the
number 3, that [1,2] is the list with 1 and 2, ... However, to get
quicker, symbols could be created at compile-time when possible (like
variables). The fact is, symbols allow compilation optimisations that
you cannot get with regular types, because the language is completely
free about their representations. Then, to the programmer it is a good
way to have a meaningful value without caring about how to represent it
in the computer. That way, while debugging, if I ask the value of
file.state I will get something I can read instead of some meaningless
integer or other anonymous object.

So I gain in readability of my code and in debugging capacity.

Pierre



More information about the Python-list mailing list