Why is `function` not a keyword in Python?

Clash Royale CLAN TAG#URR8PPP
up vote
11
down vote
favorite
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
add a comment |Â
up vote
11
down vote
favorite
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
5
functionisn't a keyword for the same reason thatstr,list, andtypearen'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
add a comment |Â
up vote
11
down vote
favorite
up vote
11
down vote
favorite
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
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
python class built-in
asked Aug 31 at 21:11
multigoodverse
1,75453264
1,75453264
5
functionisn't a keyword for the same reason thatstr,list, andtypearen'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
add a comment |Â
5
functionisn't a keyword for the same reason thatstr,list, andtypearen'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
add a comment |Â
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?
> "function" is not syntax itself, because it's easier to type "def". Why istypesyntax then when it's easy to create a type object using theclasskeyword? Let me guess. It's becausetypeis 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 classFunctionTypeis 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 likeModuleType,NoneType...
â wim
Sep 6 at 20:36
add a comment |Â
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'
add a comment |Â
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.
add a comment |Â
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?
> "function" is not syntax itself, because it's easier to type "def". Why istypesyntax then when it's easy to create a type object using theclasskeyword? Let me guess. It's becausetypeis 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 classFunctionTypeis 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 likeModuleType,NoneType...
â wim
Sep 6 at 20:36
add a comment |Â
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?
> "function" is not syntax itself, because it's easier to type "def". Why istypesyntax then when it's easy to create a type object using theclasskeyword? Let me guess. It's becausetypeis 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 classFunctionTypeis 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 likeModuleType,NoneType...
â wim
Sep 6 at 20:36
add a comment |Â
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?
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?
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 istypesyntax then when it's easy to create a type object using theclasskeyword? Let me guess. It's becausetypeis 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 classFunctionTypeis 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 likeModuleType,NoneType...
â wim
Sep 6 at 20:36
add a comment |Â
> "function" is not syntax itself, because it's easier to type "def". Why istypesyntax then when it's easy to create a type object using theclasskeyword? Let me guess. It's becausetypeis 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 classFunctionTypeis 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 likeModuleType,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
add a comment |Â
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'
add a comment |Â
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'
add a comment |Â
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'
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'
answered Aug 31 at 21:50
Ned Batchelder
246k48429560
246k48429560
add a comment |Â
add a comment |Â
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.
add a comment |Â
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.
add a comment |Â
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.
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.
edited Aug 31 at 21:29
answered Aug 31 at 21:14
Joe Iddon
13k31338
13k31338
add a comment |Â
add a comment |Â
Sign up or log in
StackExchange.ready(function ()
StackExchange.helpers.onClickDraftSave('#login-link');
);
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
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
Sign up or log in
StackExchange.ready(function ()
StackExchange.helpers.onClickDraftSave('#login-link');
);
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
Sign up or log in
StackExchange.ready(function ()
StackExchange.helpers.onClickDraftSave('#login-link');
);
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
Sign up or log in
StackExchange.ready(function ()
StackExchange.helpers.onClickDraftSave('#login-link');
);
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
5
functionisn't a keyword for the same reason thatstr,list, andtypearen'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