[Python-ideas] Object for accessing identifiers/names
Koos Zevenhoven
k7hoven at gmail.com
Wed Jun 1 15:22:39 EDT 2016
Inspired by the discussions
regarding
Guido's
"M
atch statement
brainstorm
" and
"
Quick idea: defining variables from functions..." threads, here's an idea
regarding names/identifiers.
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(), 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.
The object could be created for instance as follows:
name_object = identifier some_name
However, 'identifier' is so long that I'll use the keyword 'def' instead,
regardless of whether it is optimal or not:
name_obj = def 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
. The object would also be aware of the name 'some_name'.
T
he functionality might
work
as follows:
bool(
name_obj
)
# True if
something is
assigned to some_name
name_obj.name()
# == 'some_name'
name_obj
.set(value) #
equivalent to `some_name = value`
name_obj.get() # equivalent to just `some_name`
name_obj.unset() # like `del some_name`
Then you could pass this to a function:
func(name_obj)
Now func will be able to assign to the variable some_name, but with
some_name referring to the scope where `name_obj = def some_name` was
executed. This is similar to things that can be done with closures.
This would also allow things like:
if def some_name:
#do stuff if the name some_name is bound to something
or
if not def some_name:
some_name = something()
- Koos
-------------- next part --------------
An HTML attachment was scrubbed...
URL: <http://mail.python.org/pipermail/python-ideas/attachments/20160601/e7a19faf/attachment.html>
More information about the Python-ideas
mailing list