[Python-ideas] Object for accessing identifiers/names
Steven D'Aprano
steve at pearwood.info
Fri Jun 3 02:22:45 EDT 2016
On Wed, Jun 01, 2016 at 10:22:39PM +0300, Koos Zevenhoven wrote:
> Currently, there is no direct way to work with variable names from within
> Python. Yes, you can fiddle with __dict__s and locals() and globals(),
The usual way is to write the name as a string and operate on the
namespace.
That means that variables (names) are not themselves values: a name is
bound to a value, but there is no value which is itself a name. Instead,
we use a string as a form of indirection.
> but
> there is no convenient general way. To solve this, there could be a way
> (probably new syntax) for creating an object that
> can examine and manipulate a name binding conveniently.
Before getting too interested in the syntax for creating these, can you
explain what you want to do with them? Most uses of names don't need to
be values:
spam = 23
import spam
del spam
all work well on their own. Can you give some use-cases?
> The object could be created for instance as follows:
>
> name_object = identifier some_name
How would that be different from this?
name_object = 'some_name'
> Now this name_obj thing
> could provide functionality like assigning to the name some_name, getting
> the assigned object, and determining whether
> something has been assigned to the name or not.
Most of these things already work using strings:
# bind a name to the global scope
globals()[name_object] = 999
# lookup a name in some module's namespace
vars(module)[name_object]
# check for existence
name_object in vars(module)
What functionality do you think is missing?
--
Steve
More information about the Python-ideas
mailing list