[Python-ideas] Object for accessing identifiers/names

Michael Selik michael.selik at gmail.com
Thu Jun 2 11:28:56 EDT 2016


On Wed, Jun 1, 2016 at 3:23 PM Koos Zevenhoven <k7hoven at gmail.com> wrote:

> 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()
>




Here's an implementation. Can you show a complete example of where it would
be useful in a program?



class Identifier:
    '''
    Manipulate a variable in the current scope.
    '''

    def __init__(self, name):
        self.name = name
        self.scope = None

    def __enter__(self):
        caller = inspect.stack()[1][0]
        try:
            self.scope = caller.f_locals
            return self
        finally:
            del caller

    def __exit__(self, *args):
        self.scope = None

    def set(self, value):
        if self.scope is None:
            raise RuntimeError('must be used as a context manager')
        self.scope[self.name] = value

    def get(self):
        if self.scope is None:
            raise RuntimeError('must be used as a context manager')
        return self.scope[self.name]

    def del(self):
        if self.scope is None:
            raise RuntimeError('must be used as a context manager')
        del self.scope[self.name]

if __name__ == '__main__':
    with Identifier('x') as name_x:
        name_x.set(5)
        print(name_x.get())
    print(x)



It's a context manager because I got nervous about reference cycles as
warned by the docs of the inspect module. It might not be an issue, because
this is referencing the calling frame rather than the current frame, but
it's better to be safe than sorry.
-------------- next part --------------
An HTML attachment was scrubbed...
URL: <http://mail.python.org/pipermail/python-ideas/attachments/20160602/ca513982/attachment-0001.html>


More information about the Python-ideas mailing list