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.
python-3.x
add a comment |
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
add a comment |
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
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
python-3.x
asked Nov 28 '18 at 14:01
masiewpaomasiewpao
84110
84110
add a comment |
add a comment |
1 Answer
1
active
oldest
votes
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.openis the full name for the built-in
functionopen()
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
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
add a comment |
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
});
}
});
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
Required, but never shown
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
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.openis the full name for the built-in
functionopen()
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
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
add a comment |
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.openis the full name for the built-in
functionopen()
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
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
add a comment |
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.openis the full name for the built-in
functionopen()
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
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.openis the full name for the built-in
functionopen()
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
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
add a comment |
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
add a comment |
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.
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
Required, but never shown
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
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
Required, but never shown
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
Required, but never shown
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
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