Oh, 100% - no shade at all intended
Note there is another difference.
Slicing, or range indexing, in numpy creates a view.
In [1]: import numpy as np
In [2]: a = np.zeros(5)
In [3]: a[0] = 3
In [4]: b = a[0:2]
In [5]: b[0] = 5
In [6]: a[0]
Out[6]: 5.0
Slicing in Julia creates a copy.
julia> a = zeros(5)
5-element Vector{Float64}:
0.0
0.0
0.0
0.0
0.0
julia> b = a[1:2]
2-element Vector{Float64}:
0.0
0.0
julia> b[1] = 5
5
julia> a[1]
0.0
Unless one explicitly creates a view in Julia.
julia> c[1] = 7
7
julia> a[1]
7.0
Is this still consistent?
I know. I was just making fun of an obvious typo๐
My cat approves of this example, as do I.
code
julia-1.9> a_cat = split("""
.......................................
.......................................
...........................ไทไท..........
............................ไทไท.........
.............................ไท.........
.............................ไทไท........
..........ไทไท.................ไทไท........
..........ไทไท.................ไทไท........
.........ไทไทไทไท................ไท.........
........ไทไทไทไทไทไท...............ไท.........
........ไทไทไทไทไทไทไท.............ไทไท.........
..........ไทไทไทไทไทไทไทไทไทไทไทไทไทไทไทไทไทไทไท..........
...........ไทไทไทไทไทไทไทไทไทไทไทไทไทไทไทไทไทไท..........
...........ไทไทไทไทไทไทไทไทไทไทไทไทไทไทไทไทไทไทไท.........
...........ไทไทไทไทไทไทไทไทไทไทไทไทไทไทไทไทไทไทไท.........
............ไทไทไทไทไทไทไทไทไทไทไทไทไทไทไทไทไทไท.........
............ไทไทไทไทไทไทไทไทไทไทไทไทไทไทไทไทไทไท.........
.............ไทไทไทไทไทไทไทไทไทไทไทไทไทไทไทไทไท.........
.............ไทไทไทไทไท.....ไทไทไทไทไทไทไทไท........
.............ไทไท.ไทไท.....ไทไทไท.ไทไทไทไท........
.............ไท..ไทไท.....ไทไท....ไทไท........
............ไทไท...ไท.....ไทไท.....ไท........
............ไท....ไทไท...ไทไท......ไท........
...........ไทไท.....ไท..ไทไท......ไทไท........
.......................................
.......................................""");
julia-1.9> tiger = a_cat;
julia-1.9> fluffy = a_cat
26-element Vector{SubString{String}}:
"......................................."
"......................................."
"...........................ไทไท.........."
"............................ไทไท........."
".............................ไท........."
".............................ไทไท........"
"..........ไทไท.................ไทไท........"
"..........ไทไท.................ไทไท........"
".........ไทไทไทไท................ไท........."
"........ไทไทไทไทไทไท...............ไท........."
"........ไทไทไทไทไทไทไท.............ไทไท........."
"..........ไทไทไทไทไทไทไทไทไทไทไทไทไทไทไทไทไทไทไท.........."
"...........ไทไทไทไทไทไทไทไทไทไทไทไทไทไทไทไทไทไท.........."
"...........ไทไทไทไทไทไทไทไทไทไทไทไทไทไทไทไทไทไทไท........."
"...........ไทไทไทไทไทไทไทไทไทไทไทไทไทไทไทไทไทไทไท........."
"............ไทไทไทไทไทไทไทไทไทไทไทไทไทไทไทไทไทไท........."
"............ไทไทไทไทไทไทไทไทไทไทไทไทไทไทไทไทไทไท........."
".............ไทไทไทไทไทไทไทไทไทไทไทไทไทไทไทไทไท........."
".............ไทไทไทไทไท.....ไทไทไทไทไทไทไทไท........"
".............ไทไท.ไทไท.....ไทไทไท.ไทไทไทไท........"
".............ไท..ไทไท.....ไทไท....ไทไท........"
"............ไทไท...ไท.....ไทไท.....ไท........"
"............ไท....ไทไท...ไทไท......ไท........"
"...........ไทไท.....ไท..ไทไท......ไทไท........"
"......................................."
"......................................."
julia-1.9> tiger == fluffy
true
julia-1.9> reverse!(tiger)
26-element Vector{SubString{String}}:
"......................................."
"......................................."
"...........ไทไท.....ไท..ไทไท......ไทไท........"
"............ไท....ไทไท...ไทไท......ไท........"
"............ไทไท...ไท.....ไทไท.....ไท........"
".............ไท..ไทไท.....ไทไท....ไทไท........"
".............ไทไท.ไทไท.....ไทไทไท.ไทไทไทไท........"
".............ไทไทไทไทไท.....ไทไทไทไทไทไทไทไท........"
".............ไทไทไทไทไทไทไทไทไทไทไทไทไทไทไทไทไท........."
"............ไทไทไทไทไทไทไทไทไทไทไทไทไทไทไทไทไทไท........."
"............ไทไทไทไทไทไทไทไทไทไทไทไทไทไทไทไทไทไท........."
"...........ไทไทไทไทไทไทไทไทไทไทไทไทไทไทไทไทไทไทไท........."
"...........ไทไทไทไทไทไทไทไทไทไทไทไทไทไทไทไทไทไทไท........."
"...........ไทไทไทไทไทไทไทไทไทไทไทไทไทไทไทไทไทไท.........."
"..........ไทไทไทไทไทไทไทไทไทไทไทไทไทไทไทไทไทไทไท.........."
"........ไทไทไทไทไทไทไท.............ไทไท........."
"........ไทไทไทไทไทไท...............ไท........."
".........ไทไทไทไท................ไท........."
"..........ไทไท.................ไทไท........"
"..........ไทไท.................ไทไท........"
".............................ไทไท........"
".............................ไท........."
"............................ไทไท........."
"...........................ไทไท.........."
"......................................."
"......................................."
julia-1.9> fluffy
26-element Vector{SubString{String}}:
"......................................."
"......................................."
"...........ไทไท.....ไท..ไทไท......ไทไท........"
"............ไท....ไทไท...ไทไท......ไท........"
"............ไทไท...ไท.....ไทไท.....ไท........"
".............ไท..ไทไท.....ไทไท....ไทไท........"
".............ไทไท.ไทไท.....ไทไทไท.ไทไทไทไท........"
".............ไทไทไทไทไท.....ไทไทไทไทไทไทไทไท........"
".............ไทไทไทไทไทไทไทไทไทไทไทไทไทไทไทไทไท........."
"............ไทไทไทไทไทไทไทไทไทไทไทไทไทไทไทไทไทไท........."
"............ไทไทไทไทไทไทไทไทไทไทไทไทไทไทไทไทไทไท........."
"...........ไทไทไทไทไทไทไทไทไทไทไทไทไทไทไทไทไทไทไท........."
"...........ไทไทไทไทไทไทไทไทไทไทไทไทไทไทไทไทไทไทไท........."
"...........ไทไทไทไทไทไทไทไทไทไทไทไทไทไทไทไทไทไท.........."
"..........ไทไทไทไทไทไทไทไทไทไทไทไทไทไทไทไทไทไทไท.........."
"........ไทไทไทไทไทไทไท.............ไทไท........."
"........ไทไทไทไทไทไท...............ไท........."
".........ไทไทไทไท................ไท........."
"..........ไทไท.................ไทไท........"
"..........ไทไท.................ไทไท........"
".............................ไทไท........"
".............................ไท........."
"............................ไทไท........."
"...........................ไทไท.........."
"......................................."
"......................................."
Perhaps, out of the topic, but
Why would I have created
b
in the first place?
I often use assignment to โunpackโ structs or to keep simplicity of both usage and developing of a function. For the latter, I prefer to give clear names of the arguments to a user. But, for the developer, the names may be a bit crypty, e.g. short. Juliaโs and Pythonโs behavior of assignment helps me create interfaces comfortable for both โsidesโ, also Swift syntactically supports this.
A short example
function optimize(target_function, initial_guess)
f = target_function
x0 = initial_guess
...
end