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
bin 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