All Questions
72 questions
0
votes
2
answers
93
views
Why is the nonlocal assignment of this variable not considered a previous binding?
spam is assigned as nonlocal in the do_nonlocal() function, which, according to the Python docs, changes scope_test's binding of spam. But the docs also state that there was no previous binding for ...
-1
votes
2
answers
120
views
I can't access to the var from a for loop if it inside an if statement that is in the same scope
I need some help in my project.
the problem is that I can't access to the variable from a for loop, the variable is inside a if statement that is in the same scope.
Here is an example:
import os
...
0
votes
1
answer
24
views
Accessing variables in the local scope of a python decorator [duplicate]
Consider:
def g(value):
def f():
return value
return f
x = g(3)
x() # prints 3
Given x in the example, the returned closure from g(3), is there any way to inspect that is the value ...
-1
votes
1
answer
73
views
May i know why the below code is not errored out, even though function returning address of a local variable [duplicate]
Cpp Code:
#include <iostream>
using namespace std;
int* scopeProblem()
{
int *bret;
int b=10;
bret =&b;
return bret;
}
int main()
{
cout<<*(scopeProblem());
...
0
votes
3
answers
145
views
Having trouble figuring out params, arguments, passing variables to functions in Python
I understand this conceptually, but I can't seem to wrap my head around the syntax. Here is specifically what I'm working on - getting an average rating, from 5 inputs, and displaying an amount of ...
0
votes
1
answer
850
views
cannot access global variable after trying to modify it inside a function [duplicate]
I encounter no problems running this code:
x = 1
def func():
print(x + 1)
func()
2
But when I run this:
x = 1
def func():
try:
x += 1
except:
pass
print(x + 1)
...
-1
votes
1
answer
62
views
Why can we declare multiple identically named variables globally but not locally in C? [duplicate]
When I declare a global variable multiple times, I do not get any error.
#include <stdio.h>
int i;
int i;
int main()
{
printf("%d",i);
}
But if I declare a local variable ...
1
vote
1
answer
244
views
Are variables used in nested functions considered global?
This is a dumb question, so I apologise if so. This is for Julia, but I guess the question is not language specific.
There is advice in Julia that global variables should not be used in functions, but ...
0
votes
1
answer
110
views
How does the compiler not confuse local variable named size with function call size()?
While reading a C++ textbook, I came across this specific piece of code:
vector<double> homework;
typedef vector<double>::size_type vec_sz;
vec_sz size = homework.size();
I wonder how isn'...
0
votes
0
answers
21
views
Scope of length of a given list [duplicate]
I'm learning about scopes of python variables: global, local, nonlocal, and built-in. And I'm having trouble with identifying the scope of following variable(var_5) in each line (1,2,3 7 4).
list = [1,...
0
votes
1
answer
1k
views
Name is used in an enclosing local scope to define a local or parameter
I'm trying to test a small piece of code in a totally separate mini project and isolated just this piece, but am getting the following error:
error CS0136: A local or parameter named 'phoneNumber' ...
0
votes
0
answers
105
views
Why does locals()['var_name'] = value affect the actual local namespace?
def f():
s = 'foo'
loc = locals()
loc['s'] = 'bar'
print(s)
loc['z'] = 'baz'
print(locals())
f()
I understand that the locals function returns a copy of the local namespace....
0
votes
1
answer
341
views
Global variable priority over local variable when global function called in scope of local variable c++
I just wanted to clarify a gap in my knowledge.
Given the following code:
I would have expected that "Hi" is printed, based purely on the fact that in the scope of foo, the definition of ...
1
vote
1
answer
307
views
what is difference between scope and namespace in javascript
hi everyone . actually I'm newly learning Javascript . I read about scopes in javascript . then I read somewhere about namespace in Js and i was wondered if namespace is exactly the same as scope , so ...
0
votes
2
answers
491
views
why static local variable doesn't preserve it's value in this case?
First take a look:
#include <stdio.h>
static int v1 = 0;
int fun()
{
static int v2 = 0;
++v2 && ++v1;
printf("%i\n", v2);
return v2;
}
int main()
{
++v1;
...