Why FOR Loop last iteration ITEM not available GLOBALLY?

Can Someone help me:

I am from a python background. for loop last interation item variable is globally available in Python. But Julia throws error. Why is it so?. Is there any other way to make that last iteration item variable globally available ie., outside the FOR loop?

Please see the code in Julia & Python for reference
Julia Code:

for i in 1:8897979797897897
    try
        Char(i)
        continue
    catch
        println("Inside Loop",i)
        break
    end
end
println("Outside Loop",i)

Julia Output:
Inside Loop2097152
UndefVarError: i not defined

Stacktrace:
[1] top-level scope at In[59]:10


Python Code:

for i in range(8897979797897897):
    try:
        chr(i)
        continue
    except:
        print("Inside Loop",i)
        break
print('Outside Loop', i)

Python Output:
Inside Loop 1114112
Outside Loop 1114112


Thanks in Advance:
Santo K Thomas (Mail: santokalayil@gmail.com)

When you ask why, do you want to know scoing rules defined in the language, or the rationale for making language design choices? Here’s the rules, if that’s what you want:
https://docs.julialang.org/en/v1/manual/variables-and-scoping/

3 Likes

Thanks man… I got it…

for i in 1:8897979797897897
    try
        Char(i)
        continue
    catch
        println("Inside Loop",i)
        global x = i
        break
    end
end
println("Outside Loop",x)

As stillyslalom says:

"In general, it’s best to avoid global scope, and you could instead write a function that returns the error-producing i "

i.e.,

function errorchar(limit)
    for i in 1:limit
        try
            Char(i)
        catch 
            return i
        end
    end
end

Thank you very much Jeff_Emmanuel & stillyslalom

2 Likes

Sure

julia> for i in 1:10
           global outeri = i
           i>5 && break
       end

julia> outeri
6

As for why - it’s a design decision to make loops have their own scope. It takes some getting used to coming from python, but it ultimately leads to better practice.

Edit: ahh you beat me to it!

2 Likes

thanks man… I got it … thanks for taking time for immediate response…

In general, it’s best to avoid global scope, and you could instead write a function that returns the error-producing i:

function errorchar(limit)
    for i in 1:limit
        try
            Char(i)
        catch 
            return i
        end
    end
end
4 Likes

Is it possible that Object Oriented Programming will eventually come to Julia as well?

Probably not. Multiple dispatch is way more powerful and flexible than OOP.

4 Likes

thanks…

Multiple dispatch is a form of object orientation, just not the class-based kind. Which is a good thing.

3 Likes

Indeed. In fact, if anyone wants to have python’s way of things, they already can replicate that with Julia: simply define a struct and have all associated methods accept an instance of it as their first argument. After all in python instanceOfClass.methodcall(*args) is just a syntactic sugar of Class.methodcall(instanceOfClass, *args), and it’s easy to do the same in Julia. Class inheritance can always be replaced by composition (which can often be a better option anyway). But of course the real magic of Julia can only be unleashed when we really embrace multiple dispatch.

1 Like

I don’t think I have ever seen it used in the wild, but there is the outer keyword that you could use for this; see Scope of Variables · The Julia Language.

1 Like