이 문서는 Julia 공식 문서를 제가 정리한 것으로 원본은 다음 사이트에 있습니다.
1. 변수 이름
변수 이름에 제약이 거의 없다는 것이 julia의 특징이다.
1) $\delta$와 같은 그리스어나 한국어, 중국어도 가능하다.
2) $\delta$를 쓰고 싶으면 코드에 \delta 후에 tab을 누르면 변환하도록 한다.
$\hat{\alpha}^{2}$ 역시 \alpha-tab-\hat- tab-\^(2)-tab 조합으로 사용 가능하다.
3) 변수의 타입을 미리 선언하지 않고도 아무 값이나 넣을 수 있다.
julia> x = 1.0
1.0
julia> y = -3
-3
julia> Z = "My string"
"My string"
julia> customary_phrase = "Hello world!"
"Hello world!"
julia> UniversalDeclarationOfHumanRightsStart = "人人生而自由,在尊严和权利上一律平等。"
"人人生而自由,在尊严和权利上一律平等。"
4) 그러나 변수 시작은 a letter(A-Z or a-z), underscore(_), 00A0 이상의 유니코드를 사용해야한다.
5) 그 외에 변수 내용은 Unicode character categories Lu/Ll/Lt/Lm/Lo/Nl (letters), Sc/So (currency and other symbols), and a few other letter-like characters (e.g. a subset of the Sm math symbols)
모두 가능하다고 한다. Unicode 사이트에서 살펴보면 특이한 문자들도 가능한데 유니코드 입력 방법을 살펴보면 Numlock과 키패드가 필요해서 텐키리스 키보드를 쓰는 나는 불가능했다. (애초에 쓸 일이 있을까 싶은데 Kronecker product를 코드 상에서 구현하는 등으로 사용할 수 있다.)
6) 할당하면 안되는 경우가 있다. built-in constant인 pi와 같이 이미 원주율로 값이 정해진 경우나 built-in function sqrt 같은 경우에는 변수로 할당하려고 하면 에러가 뜬다.
julia> pi
π = 3.1415926535897...
julia> pi = 3
ERROR: cannot assign a value to imported variable Base.pi from module Main
julia> sqrt(100)
10.0
julia> sqrt = 4
ERROR: cannot assign a value to imported variable Base.sqrt from module Main
2. 임시 변수
다음과 같은 underscore 3개 조합(___)은 변수이름처럼 보이지만 파이썬처럼 버려지는 값이다.
julia> x, ___ = size([2 2; 1 1])
(2, 2)
julia> y = ___
ERROR: syntax: all-underscore identifier used as rvalue
julia> println(___)
ERROR: syntax: all-underscore identifier used as rvalue
3. Assignment vs. Mutation
변수에 어떤 값을 넣는다는 것은 assignment를 한다는 의미이다.
julia> a = [1,2,3] # an array of 3 integers
3-element Vector{Int64}:
1
2
3
julia> b = a # both b and a are names for the same array!
3-element Vector{Int64}:
1
2
3
c언어나 파이썬을 이미 해본 사람이라면 알겠지만 변수를 할당할 때 변수의 주소를 알려주는 건지, copy를 하는 건지에 따라 조심해야할 수도 있다. 위의 경우에는 a,b 변수 둘다 동일한 array를 가리키고 있다.
julia> a[1] = 42 # change the first element
42
julia> a = 3.14159 # a is now the name of a different object
3.14159
julia> b # b refers to the original array object, which has been mutated
3-element Vector{Int64}:
42
2
3
그래서 다음과 같이 a의 1번 인덱스에 값을 부여하면 b 역시 변하게 된다.
대신 a라는 변수는 3.14159라는 값으로 assign되기 때문에 a가 비록 그 array를 가리키지 않아도 array는 존재한다. 대신 b만 가리키는 것이다. 위와 같이 a[1]=value를 mutates an existing array object in memory라고 표현한다.
4. 변수/함수 이름 짓는 관습
변수 이름을 지을 때 여러가지 방법이 있다.
- camel case
- upper camel case : UpperValueCase 모든 단어 대문자로 시작
- lower camel case : upperValueCase와 같이 첫 글자 빼고 다른 단어는 모두 대문자로 시작
- pascal case = upper camel case : UpperValueCase 모든 단어 대문자로 시작
- snake case : upper_value_case와 같이 underscore(_)로 잇기
1) 변수이름은 소문자 lower case로 짓는다.
2) 단어 분리는 underscore로 쓸 수는 있지만 너무 이해가 안되는 경우가 아니라면 권장되지 않는다.
3) Type, Module은 대문자로 시작하고 단어 분리할 때는 upper camel case 사용
4) function, macro는 소문자로 하되, underscore를 안 쓴다.
5) function 이름에 느낌표(!)가 있는 경우에는 mutation or in-place function이라고 한다. 왜냐하면 함수가 call된 다음에 argument에 변화를 줄 것이기 때문이다.
'프로그래밍 Programming' 카테고리의 다른 글
[Python] 각종 라이브러리 버전체크 방법 (0) | 2024.05.16 |
---|---|
[에러기록] XlaRuntimeError: UNIMPLEMENTED: Kernel launch needs more blocks (3199360032) than allowed by hardware (2147483647). (0) | 2024.05.14 |
[Julia] Julia 외부 라이브러리 간단 사용법 (0) | 2024.03.20 |
[Julia] Julia 프로그래밍 공부자료 (0) | 2024.02.06 |
[JAX] 버전에 따른 변화 (0) | 2023.11.22 |