I did more experiments.
*Input 1:*
y = 0
begin
y = 10
end
y
*Output 1:*
10
*Input 2:*
y = 0
begin
local y = 10
end
y
*Output 2:*
0
It's the same for *if *block.
May I conclude that:
1) *Function *introduces *hard scope, *where the assignment introduce new
local variables;
2) Other blocks (including *if*, *begin-end*) introduce soft scope, where
the assignment either refers to an outer variable
or introduces an variable which can be conveyed into an outer scope;
3) The keyword *global *used in a hard scope turns the hard scope soft;
4) The keyword *local *used in a soft scope turns the soft scope hard.
By the way, I find the describe of Python's scope in Wikipedia
<http://en.wikipedia.org/wiki/Scope_%28computer_science%29#Lexical_scoping_vs._dynamic_scoping>
is quite clear, Could you write the same thing about Julia in Wikipedia?
On Tuesday, March 10, 2015 at 10:40:53 PM UTC+1, Mauro wrote:
>
> I think this is the soft vs hard scope issue. See:
> https://github.com/JuliaLang/julia/issues/9955
>
> That issue could use some fleshing out though...
>
> On Tue, 2015-03-10 at 20:03, Wendell Zheng <[email protected]
> <javascript:>> wrote:
> > *Input 1:*
> > y = 0
> > function foo()
> > y = 10
> > end
> > foo()
> > y
> >
> > *Output 1:*
> > 0
> >
> > *Input 2:*
> > y = 0
> > for i = 1:1
> > y = 10
> > end
> > y
> >
> > *Output 2:*
> > 10
> >
> > In the first example, y introduces a local variable.
> > In the second example, y is still a global variable.
> >
> > This is not consistent to what the official document said.
> >
> > I tried these examples in JuliaBox.
>
>