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












0















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.










share|improve this question



























    0















    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.










    share|improve this question

























      0












      0








      0








      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.










      share|improve this question














      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.







      python-3.x






      share|improve this question













      share|improve this question











      share|improve this question




      share|improve this question










      asked Nov 28 '18 at 14:01









      masiewpaomasiewpao

      84110




      84110
























          1 Answer
          1






          active

          oldest

          votes


















          1














          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 FilesPyCharm 2018.2.1helperspydev_pydev_commserver.py", line 34, in handle
          self.processor.process(iprot, oprot)
          File "C:Program FilesPyCharm 2018.2.1helpersthird_partythriftpy_shaded_thriftpythrift.py", line 266, in process
          self.handle_exception(e, result)
          File "C:Program FilesPyCharm 2018.2.1helpersthird_partythriftpy_shaded_thriftpythrift.py", line 254, in handle_exception
          raise e
          File "C:Program FilesPyCharm 2018.2.1helpersthird_partythriftpy_shaded_thriftpythrift.py", line 263, in process
          result.success = call()
          File "C:Program FilesPyCharm 2018.2.1helpersthird_partythriftpy_shaded_thriftpythrift.py", line 228, in call
          return f(*(args.__dict__[k] for k in api_args))
          File "C:Program FilesPyCharm 2018.2.1helperspydev_pydev_bundlepydev_console_utils.py", line 217, in getFrame
          return pydevd_thrift.frame_vars_to_struct(self.get_namespace(), hidden_ns)
          File "C:Program FilesPyCharm 2018.2.1helperspydev_pydevd_bundlepydevd_thrift.py", line 239, in frame_vars_to_struct
          keys = dict_keys(frame_f_locals)
          File "C:Program FilesPyCharm 2018.2.1helperspydev_pydevd_bundlepydevd_constants.py", line 173, in dict_keys
          return list(d.keys())
          TypeError: 'int' object is not callable





          share|improve this answer


























          • Hi, yes I'm sorry, the question is not very clear. What I was wondering is whether or not object.builtin counts as name, even though it doesn't belong to a scope. But I realise this might be quite an arbitrary thing to talk about.

            – masiewpao
            Nov 28 '18 at 14:16











          Your Answer






          StackExchange.ifUsing("editor", function () {
          StackExchange.using("externalEditor", function () {
          StackExchange.using("snippets", function () {
          StackExchange.snippets.init();
          });
          });
          }, "code-snippets");

          StackExchange.ready(function() {
          var channelOptions = {
          tags: "".split(" "),
          id: "1"
          };
          initTagRenderer("".split(" "), "".split(" "), channelOptions);

          StackExchange.using("externalEditor", function() {
          // Have to fire editor after snippets, if snippets enabled
          if (StackExchange.settings.snippets.snippetsEnabled) {
          StackExchange.using("snippets", function() {
          createEditor();
          });
          }
          else {
          createEditor();
          }
          });

          function createEditor() {
          StackExchange.prepareEditor({
          heartbeatType: 'answer',
          autoActivateHeartbeat: false,
          convertImagesToLinks: true,
          noModals: true,
          showLowRepImageUploadWarning: true,
          reputationToPostImages: 10,
          bindNavPrevention: true,
          postfix: "",
          imageUploader: {
          brandingHtml: "Powered by u003ca class="icon-imgur-white" href="https://imgur.com/"u003eu003c/au003e",
          contentPolicyHtml: "User contributions licensed under u003ca href="https://creativecommons.org/licenses/by-sa/3.0/"u003ecc by-sa 3.0 with attribution requiredu003c/au003e u003ca href="https://stackoverflow.com/legal/content-policy"u003e(content policy)u003c/au003e",
          allowUrls: true
          },
          onDemand: true,
          discardSelector: ".discard-answer"
          ,immediatelyShowMarkdownHelp:true
          });


          }
          });














          draft saved

          draft discarded


















          StackExchange.ready(
          function () {
          StackExchange.openid.initPostLogin('.new-post-login', 'https%3a%2f%2fstackoverflow.com%2fquestions%2f53521198%2fdoes-the-object-attribute-syntax-in-python-count-as-a-name%23new-answer', 'question_page');
          }
          );

          Post as a guest















          Required, but never shown

























          1 Answer
          1






          active

          oldest

          votes








          1 Answer
          1






          active

          oldest

          votes









          active

          oldest

          votes






          active

          oldest

          votes









          1














          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 FilesPyCharm 2018.2.1helperspydev_pydev_commserver.py", line 34, in handle
          self.processor.process(iprot, oprot)
          File "C:Program FilesPyCharm 2018.2.1helpersthird_partythriftpy_shaded_thriftpythrift.py", line 266, in process
          self.handle_exception(e, result)
          File "C:Program FilesPyCharm 2018.2.1helpersthird_partythriftpy_shaded_thriftpythrift.py", line 254, in handle_exception
          raise e
          File "C:Program FilesPyCharm 2018.2.1helpersthird_partythriftpy_shaded_thriftpythrift.py", line 263, in process
          result.success = call()
          File "C:Program FilesPyCharm 2018.2.1helpersthird_partythriftpy_shaded_thriftpythrift.py", line 228, in call
          return f(*(args.__dict__[k] for k in api_args))
          File "C:Program FilesPyCharm 2018.2.1helperspydev_pydev_bundlepydev_console_utils.py", line 217, in getFrame
          return pydevd_thrift.frame_vars_to_struct(self.get_namespace(), hidden_ns)
          File "C:Program FilesPyCharm 2018.2.1helperspydev_pydevd_bundlepydevd_thrift.py", line 239, in frame_vars_to_struct
          keys = dict_keys(frame_f_locals)
          File "C:Program FilesPyCharm 2018.2.1helperspydev_pydevd_bundlepydevd_constants.py", line 173, in dict_keys
          return list(d.keys())
          TypeError: 'int' object is not callable





          share|improve this answer


























          • Hi, yes I'm sorry, the question is not very clear. What I was wondering is whether or not object.builtin counts as name, even though it doesn't belong to a scope. But I realise this might be quite an arbitrary thing to talk about.

            – masiewpao
            Nov 28 '18 at 14:16
















          1














          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 FilesPyCharm 2018.2.1helperspydev_pydev_commserver.py", line 34, in handle
          self.processor.process(iprot, oprot)
          File "C:Program FilesPyCharm 2018.2.1helpersthird_partythriftpy_shaded_thriftpythrift.py", line 266, in process
          self.handle_exception(e, result)
          File "C:Program FilesPyCharm 2018.2.1helpersthird_partythriftpy_shaded_thriftpythrift.py", line 254, in handle_exception
          raise e
          File "C:Program FilesPyCharm 2018.2.1helpersthird_partythriftpy_shaded_thriftpythrift.py", line 263, in process
          result.success = call()
          File "C:Program FilesPyCharm 2018.2.1helpersthird_partythriftpy_shaded_thriftpythrift.py", line 228, in call
          return f(*(args.__dict__[k] for k in api_args))
          File "C:Program FilesPyCharm 2018.2.1helperspydev_pydev_bundlepydev_console_utils.py", line 217, in getFrame
          return pydevd_thrift.frame_vars_to_struct(self.get_namespace(), hidden_ns)
          File "C:Program FilesPyCharm 2018.2.1helperspydev_pydevd_bundlepydevd_thrift.py", line 239, in frame_vars_to_struct
          keys = dict_keys(frame_f_locals)
          File "C:Program FilesPyCharm 2018.2.1helperspydev_pydevd_bundlepydevd_constants.py", line 173, in dict_keys
          return list(d.keys())
          TypeError: 'int' object is not callable





          share|improve this answer


























          • Hi, yes I'm sorry, the question is not very clear. What I was wondering is whether or not object.builtin counts as name, even though it doesn't belong to a scope. But I realise this might be quite an arbitrary thing to talk about.

            – masiewpao
            Nov 28 '18 at 14:16














          1












          1








          1







          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 FilesPyCharm 2018.2.1helperspydev_pydev_commserver.py", line 34, in handle
          self.processor.process(iprot, oprot)
          File "C:Program FilesPyCharm 2018.2.1helpersthird_partythriftpy_shaded_thriftpythrift.py", line 266, in process
          self.handle_exception(e, result)
          File "C:Program FilesPyCharm 2018.2.1helpersthird_partythriftpy_shaded_thriftpythrift.py", line 254, in handle_exception
          raise e
          File "C:Program FilesPyCharm 2018.2.1helpersthird_partythriftpy_shaded_thriftpythrift.py", line 263, in process
          result.success = call()
          File "C:Program FilesPyCharm 2018.2.1helpersthird_partythriftpy_shaded_thriftpythrift.py", line 228, in call
          return f(*(args.__dict__[k] for k in api_args))
          File "C:Program FilesPyCharm 2018.2.1helperspydev_pydev_bundlepydev_console_utils.py", line 217, in getFrame
          return pydevd_thrift.frame_vars_to_struct(self.get_namespace(), hidden_ns)
          File "C:Program FilesPyCharm 2018.2.1helperspydev_pydevd_bundlepydevd_thrift.py", line 239, in frame_vars_to_struct
          keys = dict_keys(frame_f_locals)
          File "C:Program FilesPyCharm 2018.2.1helperspydev_pydevd_bundlepydevd_constants.py", line 173, in dict_keys
          return list(d.keys())
          TypeError: 'int' object is not callable





          share|improve this answer















          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 FilesPyCharm 2018.2.1helperspydev_pydev_commserver.py", line 34, in handle
          self.processor.process(iprot, oprot)
          File "C:Program FilesPyCharm 2018.2.1helpersthird_partythriftpy_shaded_thriftpythrift.py", line 266, in process
          self.handle_exception(e, result)
          File "C:Program FilesPyCharm 2018.2.1helpersthird_partythriftpy_shaded_thriftpythrift.py", line 254, in handle_exception
          raise e
          File "C:Program FilesPyCharm 2018.2.1helpersthird_partythriftpy_shaded_thriftpythrift.py", line 263, in process
          result.success = call()
          File "C:Program FilesPyCharm 2018.2.1helpersthird_partythriftpy_shaded_thriftpythrift.py", line 228, in call
          return f(*(args.__dict__[k] for k in api_args))
          File "C:Program FilesPyCharm 2018.2.1helperspydev_pydev_bundlepydev_console_utils.py", line 217, in getFrame
          return pydevd_thrift.frame_vars_to_struct(self.get_namespace(), hidden_ns)
          File "C:Program FilesPyCharm 2018.2.1helperspydev_pydevd_bundlepydevd_thrift.py", line 239, in frame_vars_to_struct
          keys = dict_keys(frame_f_locals)
          File "C:Program FilesPyCharm 2018.2.1helperspydev_pydevd_bundlepydevd_constants.py", line 173, in dict_keys
          return list(d.keys())
          TypeError: 'int' object is not callable






          share|improve this answer














          share|improve this answer



          share|improve this answer








          edited Nov 28 '18 at 14:11

























          answered Nov 28 '18 at 14:05









          DeepSpaceDeepSpace

          40k44778




          40k44778













          • Hi, yes I'm sorry, the question is not very clear. What I was wondering is whether or not object.builtin counts as name, even though it doesn't belong to a scope. But I realise this might be quite an arbitrary thing to talk about.

            – masiewpao
            Nov 28 '18 at 14:16



















          • Hi, yes I'm sorry, the question is not very clear. What I was wondering is whether or not object.builtin counts as name, even though it doesn't belong to a scope. But I realise this might be quite an arbitrary thing to talk about.

            – masiewpao
            Nov 28 '18 at 14:16

















          Hi, yes I'm sorry, the question is not very clear. What I was wondering is whether or not object.builtin counts as name, even though it doesn't belong to a scope. But I realise this might be quite an arbitrary thing to talk about.

          – masiewpao
          Nov 28 '18 at 14:16





          Hi, yes I'm sorry, the question is not very clear. What I was wondering is whether or not object.builtin counts as name, even though it doesn't belong to a scope. But I realise this might be quite an arbitrary thing to talk about.

          – masiewpao
          Nov 28 '18 at 14:16




















          draft saved

          draft discarded




















































          Thanks for contributing an answer to Stack Overflow!


          • Please be sure to answer the question. Provide details and share your research!

          But avoid



          • Asking for help, clarification, or responding to other answers.

          • Making statements based on opinion; back them up with references or personal experience.


          To learn more, see our tips on writing great answers.




          draft saved


          draft discarded














          StackExchange.ready(
          function () {
          StackExchange.openid.initPostLogin('.new-post-login', 'https%3a%2f%2fstackoverflow.com%2fquestions%2f53521198%2fdoes-the-object-attribute-syntax-in-python-count-as-a-name%23new-answer', 'question_page');
          }
          );

          Post as a guest















          Required, but never shown





















































          Required, but never shown














          Required, but never shown












          Required, but never shown







          Required, but never shown

































          Required, but never shown














          Required, but never shown












          Required, but never shown







          Required, but never shown