locals() built-in method in Python3 returns the variable in the global namespace
name = "Lenin Mishra"
def home():
name = "Sonu"
print(name)
home()
print(locals())
When I run the above code, Python returns me a dictionary containing the variable name, which has a value of Lenin Mishra.
{'__name__': '__main__', '__doc__': 'nExperimenting with scopen', '__package__': None, '__loader__': <_frozen_importlib_external.SourceFileLoader object at 0x10323b400>, '__spec__': None, '__annotations__': {}, '__builtins__': <module 'builtins' (built-in)>, '__file__': '/Users/leninmishra/Desktop/python_basics/scope.py', '__cached__': None, 'name': 'Lenin Mishra', 'home': <function home at 0x101db5e18>}
But as far as I understand, the variable name which has been assigned the value of Lenin Mishra is in the global scope.
Why is this happening?
python-3.x scope
add a comment |
name = "Lenin Mishra"
def home():
name = "Sonu"
print(name)
home()
print(locals())
When I run the above code, Python returns me a dictionary containing the variable name, which has a value of Lenin Mishra.
{'__name__': '__main__', '__doc__': 'nExperimenting with scopen', '__package__': None, '__loader__': <_frozen_importlib_external.SourceFileLoader object at 0x10323b400>, '__spec__': None, '__annotations__': {}, '__builtins__': <module 'builtins' (built-in)>, '__file__': '/Users/leninmishra/Desktop/python_basics/scope.py', '__cached__': None, 'name': 'Lenin Mishra', 'home': <function home at 0x101db5e18>}
But as far as I understand, the variable name which has been assigned the value of Lenin Mishra is in the global scope.
Why is this happening?
python-3.x scope
2
because you are calling it in the global scope
– juanpa.arrivillaga
Nov 21 '18 at 14:40
add a comment |
name = "Lenin Mishra"
def home():
name = "Sonu"
print(name)
home()
print(locals())
When I run the above code, Python returns me a dictionary containing the variable name, which has a value of Lenin Mishra.
{'__name__': '__main__', '__doc__': 'nExperimenting with scopen', '__package__': None, '__loader__': <_frozen_importlib_external.SourceFileLoader object at 0x10323b400>, '__spec__': None, '__annotations__': {}, '__builtins__': <module 'builtins' (built-in)>, '__file__': '/Users/leninmishra/Desktop/python_basics/scope.py', '__cached__': None, 'name': 'Lenin Mishra', 'home': <function home at 0x101db5e18>}
But as far as I understand, the variable name which has been assigned the value of Lenin Mishra is in the global scope.
Why is this happening?
python-3.x scope
name = "Lenin Mishra"
def home():
name = "Sonu"
print(name)
home()
print(locals())
When I run the above code, Python returns me a dictionary containing the variable name, which has a value of Lenin Mishra.
{'__name__': '__main__', '__doc__': 'nExperimenting with scopen', '__package__': None, '__loader__': <_frozen_importlib_external.SourceFileLoader object at 0x10323b400>, '__spec__': None, '__annotations__': {}, '__builtins__': <module 'builtins' (built-in)>, '__file__': '/Users/leninmishra/Desktop/python_basics/scope.py', '__cached__': None, 'name': 'Lenin Mishra', 'home': <function home at 0x101db5e18>}
But as far as I understand, the variable name which has been assigned the value of Lenin Mishra is in the global scope.
Why is this happening?
python-3.x scope
python-3.x scope
asked Nov 21 '18 at 14:35
Lenin Mishra
183
183
2
because you are calling it in the global scope
– juanpa.arrivillaga
Nov 21 '18 at 14:40
add a comment |
2
because you are calling it in the global scope
– juanpa.arrivillaga
Nov 21 '18 at 14:40
2
2
because you are calling it in the global scope
– juanpa.arrivillaga
Nov 21 '18 at 14:40
because you are calling it in the global scope
– juanpa.arrivillaga
Nov 21 '18 at 14:40
add a comment |
1 Answer
1
active
oldest
votes
In your code, locals() executed at global scope, so you got that output.
If you execute that locals() as part of another function, you could get the local variables to that specific function's scope.
For example, if you write your code as below, you could get out-put as empty brackets.
python works based on indentation.
Sample-code
def myfun():
print(locals())
myfun()
Output
{}
Sample code2
def home():
name = "Sonu"
print(locals())
home()
output
{'name': 'Sonu'}
This sample 2 code explains only name variable is available in its local scope.
Help of locals function says as follows:
In [1]: ?locals
Signature: locals()
Docstring:
Return a dictionary containing the current scope's local variables.
NOTE: Whether or not updates to this dictionary will affect name lookups in
the local scope and vice-versa is *implementation dependent* and not
covered by any backwards compatibility guarantees.
Type: builtin_function_or_method
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%2f53414407%2flocals-built-in-method-in-python3-returns-the-variable-in-the-global-namespace%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
In your code, locals() executed at global scope, so you got that output.
If you execute that locals() as part of another function, you could get the local variables to that specific function's scope.
For example, if you write your code as below, you could get out-put as empty brackets.
python works based on indentation.
Sample-code
def myfun():
print(locals())
myfun()
Output
{}
Sample code2
def home():
name = "Sonu"
print(locals())
home()
output
{'name': 'Sonu'}
This sample 2 code explains only name variable is available in its local scope.
Help of locals function says as follows:
In [1]: ?locals
Signature: locals()
Docstring:
Return a dictionary containing the current scope's local variables.
NOTE: Whether or not updates to this dictionary will affect name lookups in
the local scope and vice-versa is *implementation dependent* and not
covered by any backwards compatibility guarantees.
Type: builtin_function_or_method
add a comment |
In your code, locals() executed at global scope, so you got that output.
If you execute that locals() as part of another function, you could get the local variables to that specific function's scope.
For example, if you write your code as below, you could get out-put as empty brackets.
python works based on indentation.
Sample-code
def myfun():
print(locals())
myfun()
Output
{}
Sample code2
def home():
name = "Sonu"
print(locals())
home()
output
{'name': 'Sonu'}
This sample 2 code explains only name variable is available in its local scope.
Help of locals function says as follows:
In [1]: ?locals
Signature: locals()
Docstring:
Return a dictionary containing the current scope's local variables.
NOTE: Whether or not updates to this dictionary will affect name lookups in
the local scope and vice-versa is *implementation dependent* and not
covered by any backwards compatibility guarantees.
Type: builtin_function_or_method
add a comment |
In your code, locals() executed at global scope, so you got that output.
If you execute that locals() as part of another function, you could get the local variables to that specific function's scope.
For example, if you write your code as below, you could get out-put as empty brackets.
python works based on indentation.
Sample-code
def myfun():
print(locals())
myfun()
Output
{}
Sample code2
def home():
name = "Sonu"
print(locals())
home()
output
{'name': 'Sonu'}
This sample 2 code explains only name variable is available in its local scope.
Help of locals function says as follows:
In [1]: ?locals
Signature: locals()
Docstring:
Return a dictionary containing the current scope's local variables.
NOTE: Whether or not updates to this dictionary will affect name lookups in
the local scope and vice-versa is *implementation dependent* and not
covered by any backwards compatibility guarantees.
Type: builtin_function_or_method
In your code, locals() executed at global scope, so you got that output.
If you execute that locals() as part of another function, you could get the local variables to that specific function's scope.
For example, if you write your code as below, you could get out-put as empty brackets.
python works based on indentation.
Sample-code
def myfun():
print(locals())
myfun()
Output
{}
Sample code2
def home():
name = "Sonu"
print(locals())
home()
output
{'name': 'Sonu'}
This sample 2 code explains only name variable is available in its local scope.
Help of locals function says as follows:
In [1]: ?locals
Signature: locals()
Docstring:
Return a dictionary containing the current scope's local variables.
NOTE: Whether or not updates to this dictionary will affect name lookups in
the local scope and vice-versa is *implementation dependent* and not
covered by any backwards compatibility guarantees.
Type: builtin_function_or_method
edited Nov 21 '18 at 16:57
answered Nov 21 '18 at 14:57
Haranadh
77731329
77731329
add a comment |
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.
Some of your past answers have not been well-received, and you're in danger of being blocked from answering.
Please pay close attention to the following guidance:
- 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%2f53414407%2flocals-built-in-method-in-python3-returns-the-variable-in-the-global-namespace%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
2
because you are calling it in the global scope
– juanpa.arrivillaga
Nov 21 '18 at 14:40