Why is `function` not a keyword in Python?

The name of the pictureThe name of the pictureThe name of the pictureClash Royale CLAN TAG#URR8PPP











up vote
11
down vote

favorite
4












Classes like str or type



>>> type("pear")
<class 'str'>
>>> type(str)
<class 'type'>


are accessible in Python:



>>> str
<class 'str'>
>>> type
<class 'type'>


However, classes function and builtin_function_or_method are not.



>>> def foo(): pass
...
>>> type(foo)
<class 'function'>
>>> type(print)
<class 'builtin_function_or_method'>


They appear as builtin classes but trying to access them throws name errors:



>>> function
Traceback (most recent call last):
File "<stdin>", line 1, in <module>
NameError: name 'function' is not defined
>>> builtin_function_or_method
Traceback (most recent call last):
File "<stdin>", line 1, in <module>
NameError: name 'builtin_function_or_method' is not defined


What's special about function and builtin_function_or_method ?










share|improve this question

















  • 5




    function isn't a keyword for the same reason that str, list, and type aren't. (You probably meant to ask why that identifier isn't defined, but the distinction between keywords and globally-defined identifiers is a big one.)
    – Nic Hartley
    Sep 1 at 1:44















up vote
11
down vote

favorite
4












Classes like str or type



>>> type("pear")
<class 'str'>
>>> type(str)
<class 'type'>


are accessible in Python:



>>> str
<class 'str'>
>>> type
<class 'type'>


However, classes function and builtin_function_or_method are not.



>>> def foo(): pass
...
>>> type(foo)
<class 'function'>
>>> type(print)
<class 'builtin_function_or_method'>


They appear as builtin classes but trying to access them throws name errors:



>>> function
Traceback (most recent call last):
File "<stdin>", line 1, in <module>
NameError: name 'function' is not defined
>>> builtin_function_or_method
Traceback (most recent call last):
File "<stdin>", line 1, in <module>
NameError: name 'builtin_function_or_method' is not defined


What's special about function and builtin_function_or_method ?










share|improve this question

















  • 5




    function isn't a keyword for the same reason that str, list, and type aren't. (You probably meant to ask why that identifier isn't defined, but the distinction between keywords and globally-defined identifiers is a big one.)
    – Nic Hartley
    Sep 1 at 1:44













up vote
11
down vote

favorite
4









up vote
11
down vote

favorite
4






4





Classes like str or type



>>> type("pear")
<class 'str'>
>>> type(str)
<class 'type'>


are accessible in Python:



>>> str
<class 'str'>
>>> type
<class 'type'>


However, classes function and builtin_function_or_method are not.



>>> def foo(): pass
...
>>> type(foo)
<class 'function'>
>>> type(print)
<class 'builtin_function_or_method'>


They appear as builtin classes but trying to access them throws name errors:



>>> function
Traceback (most recent call last):
File "<stdin>", line 1, in <module>
NameError: name 'function' is not defined
>>> builtin_function_or_method
Traceback (most recent call last):
File "<stdin>", line 1, in <module>
NameError: name 'builtin_function_or_method' is not defined


What's special about function and builtin_function_or_method ?










share|improve this question













Classes like str or type



>>> type("pear")
<class 'str'>
>>> type(str)
<class 'type'>


are accessible in Python:



>>> str
<class 'str'>
>>> type
<class 'type'>


However, classes function and builtin_function_or_method are not.



>>> def foo(): pass
...
>>> type(foo)
<class 'function'>
>>> type(print)
<class 'builtin_function_or_method'>


They appear as builtin classes but trying to access them throws name errors:



>>> function
Traceback (most recent call last):
File "<stdin>", line 1, in <module>
NameError: name 'function' is not defined
>>> builtin_function_or_method
Traceback (most recent call last):
File "<stdin>", line 1, in <module>
NameError: name 'builtin_function_or_method' is not defined


What's special about function and builtin_function_or_method ?







python class built-in






share|improve this question













share|improve this question











share|improve this question




share|improve this question










asked Aug 31 at 21:11









multigoodverse

1,75453264




1,75453264







  • 5




    function isn't a keyword for the same reason that str, list, and type aren't. (You probably meant to ask why that identifier isn't defined, but the distinction between keywords and globally-defined identifiers is a big one.)
    – Nic Hartley
    Sep 1 at 1:44













  • 5




    function isn't a keyword for the same reason that str, list, and type aren't. (You probably meant to ask why that identifier isn't defined, but the distinction between keywords and globally-defined identifiers is a big one.)
    – Nic Hartley
    Sep 1 at 1:44








5




5




function isn't a keyword for the same reason that str, list, and type aren't. (You probably meant to ask why that identifier isn't defined, but the distinction between keywords and globally-defined identifiers is a big one.)
– Nic Hartley
Sep 1 at 1:44





function isn't a keyword for the same reason that str, list, and type aren't. (You probably meant to ask why that identifier isn't defined, but the distinction between keywords and globally-defined identifiers is a big one.)
– Nic Hartley
Sep 1 at 1:44













3 Answers
3






active

oldest

votes

















up vote
13
down vote













What you are seeing is the representation of the function type:



>>> from types import FunctionType
>>> repr(FunctionType)
"<class 'function'>"


This is the "type" of a function defined with def or otherwise:



>>> def f():
... pass
...
>>> type(f) is FunctionType
True
>>> type(lambda: None) is FunctionType
True


"function" is not syntax itself, because it's easier to type "def".



A rhetorical question: if def was the name used to resolve to the function type, then what syntax would you use to actually define a function?






share|improve this answer






















  • > "function" is not syntax itself, because it's easier to type "def". Why is type syntax then when it's easy to create a type object using the class keyword? Let me guess. It's because type is also used to check the type of an object.
    – multigoodverse
    Sep 3 at 15:35










  • That one probably predates the unification of types and classes in Python (classes were added to the language later)
    – wim
    Sep 3 at 20:19










  • Lol. I was going to see how the class FunctionType is constructed in python and I come up with this: def _f(): pass; FunctionType = type(_f)
    – J. C. Rocamonde
    Sep 6 at 20:20










  • It's ironic. I guess that a function's properties as an object are not defined in the stdlib but as part of the C code in CPython's implementation. Is this correct?
    – J. C. Rocamonde
    Sep 6 at 20:20










  • Yes. You will find similar tautological definitions for things like ModuleType, NoneType ...
    – wim
    Sep 6 at 20:36

















up vote
4
down vote













Classes and functions have an inherent name:



>>> def foo():
... pass
...
>>> foo
<function foo at 0x10f951400>
>>> foo.__name__
'foo'


The name attached to the object is independent of the name you use to access the object, though when defining functions (and classes), they are the same.



>>> bar = foo
>>> bar
<function foo at 0x10f951400>


You can even get rid of the variable you use to access the function, as long as you have a reference somewhere else:



>>> funcs = [foo]
>>> funcs[0]
<function foo at 0x10f951400>
>>> del foo
>>> foo
Traceback (most recent call last):
File "<stdin>", line 1, in <module>
NameError: name 'foo' is not defined
>>> funcs[0]
<function foo at 0x10f951400>
>>> funcs[0].__name__
'foo'


The built-in function type is like this: there is no variable that refers to it, but it still exists, and has a __name__:



>>> def foo(): pass
...
>>> type(foo)
<class 'function'>
>>> type(foo).__name__
'function'





share|improve this answer



























    up vote
    3
    down vote













    A Type Object is returned from the type() function (or object.__class__).



    You can get a full list of these type objects here.



    But you can also create your own:



    >>> import types
    >>> types.new_class('my_made_up_type')
    <class 'types.my_made_up_type'>


    So it is a type object that certain objects (like built-in functions) return (namely: types.BuiltinFunctionType) , but these type objects are not built-in classes, so it is no wonder they are not defined in the interpreter.






    share|improve this answer






















      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',
      convertImagesToLinks: true,
      noModals: false,
      showLowRepImageUploadWarning: true,
      reputationToPostImages: 10,
      bindNavPrevention: true,
      postfix: "",
      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%2f52122632%2fwhy-is-function-not-a-keyword-in-python%23new-answer', 'question_page');

      );

      Post as a guest






























      3 Answers
      3






      active

      oldest

      votes








      3 Answers
      3






      active

      oldest

      votes









      active

      oldest

      votes






      active

      oldest

      votes








      up vote
      13
      down vote













      What you are seeing is the representation of the function type:



      >>> from types import FunctionType
      >>> repr(FunctionType)
      "<class 'function'>"


      This is the "type" of a function defined with def or otherwise:



      >>> def f():
      ... pass
      ...
      >>> type(f) is FunctionType
      True
      >>> type(lambda: None) is FunctionType
      True


      "function" is not syntax itself, because it's easier to type "def".



      A rhetorical question: if def was the name used to resolve to the function type, then what syntax would you use to actually define a function?






      share|improve this answer






















      • > "function" is not syntax itself, because it's easier to type "def". Why is type syntax then when it's easy to create a type object using the class keyword? Let me guess. It's because type is also used to check the type of an object.
        – multigoodverse
        Sep 3 at 15:35










      • That one probably predates the unification of types and classes in Python (classes were added to the language later)
        – wim
        Sep 3 at 20:19










      • Lol. I was going to see how the class FunctionType is constructed in python and I come up with this: def _f(): pass; FunctionType = type(_f)
        – J. C. Rocamonde
        Sep 6 at 20:20










      • It's ironic. I guess that a function's properties as an object are not defined in the stdlib but as part of the C code in CPython's implementation. Is this correct?
        – J. C. Rocamonde
        Sep 6 at 20:20










      • Yes. You will find similar tautological definitions for things like ModuleType, NoneType ...
        – wim
        Sep 6 at 20:36














      up vote
      13
      down vote













      What you are seeing is the representation of the function type:



      >>> from types import FunctionType
      >>> repr(FunctionType)
      "<class 'function'>"


      This is the "type" of a function defined with def or otherwise:



      >>> def f():
      ... pass
      ...
      >>> type(f) is FunctionType
      True
      >>> type(lambda: None) is FunctionType
      True


      "function" is not syntax itself, because it's easier to type "def".



      A rhetorical question: if def was the name used to resolve to the function type, then what syntax would you use to actually define a function?






      share|improve this answer






















      • > "function" is not syntax itself, because it's easier to type "def". Why is type syntax then when it's easy to create a type object using the class keyword? Let me guess. It's because type is also used to check the type of an object.
        – multigoodverse
        Sep 3 at 15:35










      • That one probably predates the unification of types and classes in Python (classes were added to the language later)
        – wim
        Sep 3 at 20:19










      • Lol. I was going to see how the class FunctionType is constructed in python and I come up with this: def _f(): pass; FunctionType = type(_f)
        – J. C. Rocamonde
        Sep 6 at 20:20










      • It's ironic. I guess that a function's properties as an object are not defined in the stdlib but as part of the C code in CPython's implementation. Is this correct?
        – J. C. Rocamonde
        Sep 6 at 20:20










      • Yes. You will find similar tautological definitions for things like ModuleType, NoneType ...
        – wim
        Sep 6 at 20:36












      up vote
      13
      down vote










      up vote
      13
      down vote









      What you are seeing is the representation of the function type:



      >>> from types import FunctionType
      >>> repr(FunctionType)
      "<class 'function'>"


      This is the "type" of a function defined with def or otherwise:



      >>> def f():
      ... pass
      ...
      >>> type(f) is FunctionType
      True
      >>> type(lambda: None) is FunctionType
      True


      "function" is not syntax itself, because it's easier to type "def".



      A rhetorical question: if def was the name used to resolve to the function type, then what syntax would you use to actually define a function?






      share|improve this answer














      What you are seeing is the representation of the function type:



      >>> from types import FunctionType
      >>> repr(FunctionType)
      "<class 'function'>"


      This is the "type" of a function defined with def or otherwise:



      >>> def f():
      ... pass
      ...
      >>> type(f) is FunctionType
      True
      >>> type(lambda: None) is FunctionType
      True


      "function" is not syntax itself, because it's easier to type "def".



      A rhetorical question: if def was the name used to resolve to the function type, then what syntax would you use to actually define a function?







      share|improve this answer














      share|improve this answer



      share|improve this answer








      edited Aug 31 at 21:24

























      answered Aug 31 at 21:15









      wim

      147k42276397




      147k42276397











      • > "function" is not syntax itself, because it's easier to type "def". Why is type syntax then when it's easy to create a type object using the class keyword? Let me guess. It's because type is also used to check the type of an object.
        – multigoodverse
        Sep 3 at 15:35










      • That one probably predates the unification of types and classes in Python (classes were added to the language later)
        – wim
        Sep 3 at 20:19










      • Lol. I was going to see how the class FunctionType is constructed in python and I come up with this: def _f(): pass; FunctionType = type(_f)
        – J. C. Rocamonde
        Sep 6 at 20:20










      • It's ironic. I guess that a function's properties as an object are not defined in the stdlib but as part of the C code in CPython's implementation. Is this correct?
        – J. C. Rocamonde
        Sep 6 at 20:20










      • Yes. You will find similar tautological definitions for things like ModuleType, NoneType ...
        – wim
        Sep 6 at 20:36
















      • > "function" is not syntax itself, because it's easier to type "def". Why is type syntax then when it's easy to create a type object using the class keyword? Let me guess. It's because type is also used to check the type of an object.
        – multigoodverse
        Sep 3 at 15:35










      • That one probably predates the unification of types and classes in Python (classes were added to the language later)
        – wim
        Sep 3 at 20:19










      • Lol. I was going to see how the class FunctionType is constructed in python and I come up with this: def _f(): pass; FunctionType = type(_f)
        – J. C. Rocamonde
        Sep 6 at 20:20










      • It's ironic. I guess that a function's properties as an object are not defined in the stdlib but as part of the C code in CPython's implementation. Is this correct?
        – J. C. Rocamonde
        Sep 6 at 20:20










      • Yes. You will find similar tautological definitions for things like ModuleType, NoneType ...
        – wim
        Sep 6 at 20:36















      > "function" is not syntax itself, because it's easier to type "def". Why is type syntax then when it's easy to create a type object using the class keyword? Let me guess. It's because type is also used to check the type of an object.
      – multigoodverse
      Sep 3 at 15:35




      > "function" is not syntax itself, because it's easier to type "def". Why is type syntax then when it's easy to create a type object using the class keyword? Let me guess. It's because type is also used to check the type of an object.
      – multigoodverse
      Sep 3 at 15:35












      That one probably predates the unification of types and classes in Python (classes were added to the language later)
      – wim
      Sep 3 at 20:19




      That one probably predates the unification of types and classes in Python (classes were added to the language later)
      – wim
      Sep 3 at 20:19












      Lol. I was going to see how the class FunctionType is constructed in python and I come up with this: def _f(): pass; FunctionType = type(_f)
      – J. C. Rocamonde
      Sep 6 at 20:20




      Lol. I was going to see how the class FunctionType is constructed in python and I come up with this: def _f(): pass; FunctionType = type(_f)
      – J. C. Rocamonde
      Sep 6 at 20:20












      It's ironic. I guess that a function's properties as an object are not defined in the stdlib but as part of the C code in CPython's implementation. Is this correct?
      – J. C. Rocamonde
      Sep 6 at 20:20




      It's ironic. I guess that a function's properties as an object are not defined in the stdlib but as part of the C code in CPython's implementation. Is this correct?
      – J. C. Rocamonde
      Sep 6 at 20:20












      Yes. You will find similar tautological definitions for things like ModuleType, NoneType ...
      – wim
      Sep 6 at 20:36




      Yes. You will find similar tautological definitions for things like ModuleType, NoneType ...
      – wim
      Sep 6 at 20:36












      up vote
      4
      down vote













      Classes and functions have an inherent name:



      >>> def foo():
      ... pass
      ...
      >>> foo
      <function foo at 0x10f951400>
      >>> foo.__name__
      'foo'


      The name attached to the object is independent of the name you use to access the object, though when defining functions (and classes), they are the same.



      >>> bar = foo
      >>> bar
      <function foo at 0x10f951400>


      You can even get rid of the variable you use to access the function, as long as you have a reference somewhere else:



      >>> funcs = [foo]
      >>> funcs[0]
      <function foo at 0x10f951400>
      >>> del foo
      >>> foo
      Traceback (most recent call last):
      File "<stdin>", line 1, in <module>
      NameError: name 'foo' is not defined
      >>> funcs[0]
      <function foo at 0x10f951400>
      >>> funcs[0].__name__
      'foo'


      The built-in function type is like this: there is no variable that refers to it, but it still exists, and has a __name__:



      >>> def foo(): pass
      ...
      >>> type(foo)
      <class 'function'>
      >>> type(foo).__name__
      'function'





      share|improve this answer
























        up vote
        4
        down vote













        Classes and functions have an inherent name:



        >>> def foo():
        ... pass
        ...
        >>> foo
        <function foo at 0x10f951400>
        >>> foo.__name__
        'foo'


        The name attached to the object is independent of the name you use to access the object, though when defining functions (and classes), they are the same.



        >>> bar = foo
        >>> bar
        <function foo at 0x10f951400>


        You can even get rid of the variable you use to access the function, as long as you have a reference somewhere else:



        >>> funcs = [foo]
        >>> funcs[0]
        <function foo at 0x10f951400>
        >>> del foo
        >>> foo
        Traceback (most recent call last):
        File "<stdin>", line 1, in <module>
        NameError: name 'foo' is not defined
        >>> funcs[0]
        <function foo at 0x10f951400>
        >>> funcs[0].__name__
        'foo'


        The built-in function type is like this: there is no variable that refers to it, but it still exists, and has a __name__:



        >>> def foo(): pass
        ...
        >>> type(foo)
        <class 'function'>
        >>> type(foo).__name__
        'function'





        share|improve this answer






















          up vote
          4
          down vote










          up vote
          4
          down vote









          Classes and functions have an inherent name:



          >>> def foo():
          ... pass
          ...
          >>> foo
          <function foo at 0x10f951400>
          >>> foo.__name__
          'foo'


          The name attached to the object is independent of the name you use to access the object, though when defining functions (and classes), they are the same.



          >>> bar = foo
          >>> bar
          <function foo at 0x10f951400>


          You can even get rid of the variable you use to access the function, as long as you have a reference somewhere else:



          >>> funcs = [foo]
          >>> funcs[0]
          <function foo at 0x10f951400>
          >>> del foo
          >>> foo
          Traceback (most recent call last):
          File "<stdin>", line 1, in <module>
          NameError: name 'foo' is not defined
          >>> funcs[0]
          <function foo at 0x10f951400>
          >>> funcs[0].__name__
          'foo'


          The built-in function type is like this: there is no variable that refers to it, but it still exists, and has a __name__:



          >>> def foo(): pass
          ...
          >>> type(foo)
          <class 'function'>
          >>> type(foo).__name__
          'function'





          share|improve this answer












          Classes and functions have an inherent name:



          >>> def foo():
          ... pass
          ...
          >>> foo
          <function foo at 0x10f951400>
          >>> foo.__name__
          'foo'


          The name attached to the object is independent of the name you use to access the object, though when defining functions (and classes), they are the same.



          >>> bar = foo
          >>> bar
          <function foo at 0x10f951400>


          You can even get rid of the variable you use to access the function, as long as you have a reference somewhere else:



          >>> funcs = [foo]
          >>> funcs[0]
          <function foo at 0x10f951400>
          >>> del foo
          >>> foo
          Traceback (most recent call last):
          File "<stdin>", line 1, in <module>
          NameError: name 'foo' is not defined
          >>> funcs[0]
          <function foo at 0x10f951400>
          >>> funcs[0].__name__
          'foo'


          The built-in function type is like this: there is no variable that refers to it, but it still exists, and has a __name__:



          >>> def foo(): pass
          ...
          >>> type(foo)
          <class 'function'>
          >>> type(foo).__name__
          'function'






          share|improve this answer












          share|improve this answer



          share|improve this answer










          answered Aug 31 at 21:50









          Ned Batchelder

          246k48429560




          246k48429560




















              up vote
              3
              down vote













              A Type Object is returned from the type() function (or object.__class__).



              You can get a full list of these type objects here.



              But you can also create your own:



              >>> import types
              >>> types.new_class('my_made_up_type')
              <class 'types.my_made_up_type'>


              So it is a type object that certain objects (like built-in functions) return (namely: types.BuiltinFunctionType) , but these type objects are not built-in classes, so it is no wonder they are not defined in the interpreter.






              share|improve this answer


























                up vote
                3
                down vote













                A Type Object is returned from the type() function (or object.__class__).



                You can get a full list of these type objects here.



                But you can also create your own:



                >>> import types
                >>> types.new_class('my_made_up_type')
                <class 'types.my_made_up_type'>


                So it is a type object that certain objects (like built-in functions) return (namely: types.BuiltinFunctionType) , but these type objects are not built-in classes, so it is no wonder they are not defined in the interpreter.






                share|improve this answer
























                  up vote
                  3
                  down vote










                  up vote
                  3
                  down vote









                  A Type Object is returned from the type() function (or object.__class__).



                  You can get a full list of these type objects here.



                  But you can also create your own:



                  >>> import types
                  >>> types.new_class('my_made_up_type')
                  <class 'types.my_made_up_type'>


                  So it is a type object that certain objects (like built-in functions) return (namely: types.BuiltinFunctionType) , but these type objects are not built-in classes, so it is no wonder they are not defined in the interpreter.






                  share|improve this answer














                  A Type Object is returned from the type() function (or object.__class__).



                  You can get a full list of these type objects here.



                  But you can also create your own:



                  >>> import types
                  >>> types.new_class('my_made_up_type')
                  <class 'types.my_made_up_type'>


                  So it is a type object that certain objects (like built-in functions) return (namely: types.BuiltinFunctionType) , but these type objects are not built-in classes, so it is no wonder they are not defined in the interpreter.







                  share|improve this answer














                  share|improve this answer



                  share|improve this answer








                  edited Aug 31 at 21:29

























                  answered Aug 31 at 21:14









                  Joe Iddon

                  13k31338




                  13k31338



























                       

                      draft saved


                      draft discarded















































                       


                      draft saved


                      draft discarded














                      StackExchange.ready(
                      function ()
                      StackExchange.openid.initPostLogin('.new-post-login', 'https%3a%2f%2fstackoverflow.com%2fquestions%2f52122632%2fwhy-is-function-not-a-keyword-in-python%23new-answer', 'question_page');

                      );

                      Post as a guest













































































                      這個網誌中的熱門文章

                      tkz-euclide: tkzDrawCircle[R] not working

                      How to combine Bézier curves to a surface?

                      1st Magritte Awards