用法:
=
=
是賦值運算符。
- 對於變量
a
和表達式b
,a = b
使a
引用b
的值。 - 對於函數
f(x)
,f(x) = x
定義一個新的函數常量f
,或者如果f
已經定義,則向f
添加一個新方法;此用法等效於function f(x); x; end
。 a[i] = v
調用setindex!
(a,v,i)
。a.b = c
調用setproperty!
(a,:b,c)
。- 在函數調用中,
f(a=b)
將b
作為關鍵字參數a
的值傳遞。 - 在帶逗號的括號內,
(a=1,)
構造一個NamedTuple
例子
將 a
分配給 b
不會創建 b
的副本;而是使用
或 copy
。deepcopy
julia> b = [1]; a = b; b[1] = 2; a
1-element Array{Int64, 1}:
2
julia> b = [1]; a = copy(b); b[1] = 2; a
1-element Array{Int64, 1}:
1
傳遞給函數的集合也不會被複製。函數可以修改(變異)其參數所引用的對象的內容。 (執行此操作的函數名稱通常以'!' 為後綴。)
julia> function f!(x); x[:] .+= 1; end
f! (generic function with 1 method)
julia> a = [1]; f!(a); a
1-element Array{Int64, 1}:
2
賦值可以並行操作多個變量,從一個可迭代對象中獲取值:
julia> a, b = 4, 5
(4, 5)
julia> a, b = 1:3
1:3
julia> a, b
(1, 2)
賦值可以對多個變量串聯操作,並會返回right-hand-most表達式的值:
julia> a = [1]; b = [2]; c = [3]; a = b = c
1-element Array{Int64, 1}:
3
julia> b[1] = 2; a, b, c
([2], [2], [2])
越界索引處的分配不會增加集合。如果集合是
,則可以使用 Vector
或 push!
來種植它。append!
julia> a = [1, 1]; a[3] = 2
ERROR: BoundsError: attempt to access 2-element Array{Int64, 1} at index [3]
[...]
julia> push!(a, 2, 3)
4-element Array{Int64, 1}:
1
1
2
3
分配[]
不會從集合中消除元素;而是使用
。filter!
julia> a = collect(1:3); a[a .<= 1] = []
ERROR: DimensionMismatch("tried to assign 0 elements to 1 destinations")
[...]
julia> filter!(x -> x > 1, a) # in-place & thus more efficient than a = a[a .> 1]
2-element Array{Int64, 1}:
2
3
相關用法
- Julia splice!用法及代碼示例
- Julia @cfunction用法及代碼示例
- Julia LibGit2.count用法及代碼示例
- Julia LinearAlgebra.BLAS.dot用法及代碼示例
- Julia break用法及代碼示例
- Julia sizeof()用法及代碼示例
- Julia :<=用法及代碼示例
- Julia zero()用法及代碼示例
- Julia rem用法及代碼示例
- Julia ...用法及代碼示例
- Julia setfield()用法及代碼示例
- Julia rpad用法及代碼示例
- Julia sort用法及代碼示例
- Julia tail用法及代碼示例
- Julia cis方法用法及代碼示例
- Julia SparseArrays.spdiagm用法及代碼示例
- Julia Distributed.procs方法用法及代碼示例
- Julia Filesystem.mkpath用法及代碼示例
- Julia cld用法及代碼示例
- Julia sqrt方法用法及代碼示例
- Julia LinearAlgebra.bunchkaufman用法及代碼示例
- Julia union!用法及代碼示例
- Julia Iterators.partition用法及代碼示例
- Julia findfirst方法用法及代碼示例
- Julia Test.@test_skip用法及代碼示例
注:本文由純淨天空篩選整理自julialang.org 大神的英文原創作品 = — Keyword。非經特殊聲明,原始代碼版權歸原作者所有,本譯文未經允許或授權,請勿轉載或複製。