masiewpao
masiewpao

Reputation: 309

Does the object.attribute syntax in python count as a name?

I am wondering if something counts as a name if it is expressed as an object.attribute syntax. The motivation comes from trying to understand this code from Learning Python:

def makeopen(id):
    original = builtins.open
    def custom(*pargs, **kargs):
        print('Custom open call %r' %id, pargs, kargs)
        return original(*pargs,*kargs)
    builtins.open(custom)

I wanted to map out each name/variable to the scope that they exist in. I am unsure what to do with builtins.open. Is builtins.open a name? In the book the author does state that object.attribute lookup follows completely different rules to plain looksups, which would mean to me that builtins.open is not a name at all, since the execution model docs say that scopes define where names are visible. Since object.attribute syntax is visible in any scope, it doesn't fit into this classification and is not a name.

However the conceptual problem I have is then defining what builtins.open is? It is still a reference to an object, and can be rebound to any other object. In that sense it is a name, even although it doesn't follow scope rules?

Thank you.

Upvotes: 0

Views: 23

Answers (1)

DeepSpace
DeepSpace

Reputation: 81624

builtins.open is just another way to access the global open function:

import builtins

print(open)
#  <built-in function open>
print(builtins.open)
#  <built-in function open>
print(open == builtins.open)
#  True

From the docs:

This module provides direct access to all ‘built-in’ identifiers of Python; for example, builtins.open is the full name for the built-in function open()

Regarding the second part of your question, I'm not sure what you mean. (Almost) every "name" in Python can be reassigned to something completely different.

>>> list
<class 'list'>
>>> list = 1
>>> list
1

However, everything under builtins is protected, otherwise some nasty weird behavior was bound to happen in case someone(thing) reassigned its attributes during runtime.

>>> import builtins
>>> builtins.list = 1

   Traceback (most recent call last):
  File "C:\Program Files\PyCharm 2018.2.1\helpers\pydev\_pydev_comm\server.py", line 34, in handle
    self.processor.process(iprot, oprot)
  File "C:\Program Files\PyCharm 2018.2.1\helpers\third_party\thriftpy\_shaded_thriftpy\thrift.py", line 266, in process
    self.handle_exception(e, result)
  File "C:\Program Files\PyCharm 2018.2.1\helpers\third_party\thriftpy\_shaded_thriftpy\thrift.py", line 254, in handle_exception
    raise e
  File "C:\Program Files\PyCharm 2018.2.1\helpers\third_party\thriftpy\_shaded_thriftpy\thrift.py", line 263, in process
    result.success = call()
  File "C:\Program Files\PyCharm 2018.2.1\helpers\third_party\thriftpy\_shaded_thriftpy\thrift.py", line 228, in call
    return f(*(args.__dict__[k] for k in api_args))
  File "C:\Program Files\PyCharm 2018.2.1\helpers\pydev\_pydev_bundle\pydev_console_utils.py", line 217, in getFrame
    return pydevd_thrift.frame_vars_to_struct(self.get_namespace(), hidden_ns)
  File "C:\Program Files\PyCharm 2018.2.1\helpers\pydev\_pydevd_bundle\pydevd_thrift.py", line 239, in frame_vars_to_struct
    keys = dict_keys(frame_f_locals)
  File "C:\Program Files\PyCharm 2018.2.1\helpers\pydev\_pydevd_bundle\pydevd_constants.py", line 173, in dict_keys
    return list(d.keys())
TypeError: 'int' object is not callable

Upvotes: 1

Related Questions