Proposal for adding symbols within Python

Steven D'Aprano steve at REMOVEMEcyber.com.au
Mon Nov 14 21:50:04 EST 2005


Björn Lindström wrote:

> So, I guess no one read my explanation of why this an issue about more
> than implementing enums (which is fairly trivial, as we have seen).


I read it. I don't see that it is an issue, and I 
especially don't see why it is relevent to Pierre's 
usage of symbols.

In your earlier post, you say:

"The problem with that is that you can't pass around 
the names of objects that are used for other things."

That's demonstrably not true. If you know that the name 
of something is Parrot, then you can pass the string 
"Parrot" and use it in many ways:

print obj.__getattribute__["Parrot"]
instance.__dict__["Parrot"] = 42

I'm not aware of anything that you can do to a 
name/object binding that can't also be done by a 
string. Perhaps I've missed something -- anyone?

If you don't know the name, well, how did it get into 
your program in the first case? Where did it come from? 
If it came from user input, then surely that is a 
string, yes?

You also suggested:

"Being able to do that precludes the need for 
converting going back and forth between strings and 
method names when you need to do things like keeping a 
list of function names, even when you need to be able 
to change what those function names point to."

I'm not convinced. Instead of keeping a list of 
function names, just keep a list of functions -- 
functions are first-class objects in Python.

If you need to change what the function names point to, 
simply rebind the list item to another function.

The only benefit I can see for being able to refer to 
functions by name would be if you are writing a formula 
evaluator, it might be useful to have "sin"(0) evaluate 
  directly. But I don't like the idea of making strings 
aliases to executables, except through a single 
well-understood mechanism. I'd much rather accept one 
intermediate layer than create a second mechanism of 
function execution:

table = {"sin": math.sin, "cos": math.cos}
# easy to modify
table["sin"] = my_better_sine_function
result = table["sin"](0)


If I have missed a usage case, perhaps you should give 
at specific example.


-- 
Steven.




More information about the Python-list mailing list