Operands의 Data type에 따라 연산자의 동작이 달라지는 대표적인 예임.https://www.codingeek.com/tutorials/python/operator-overloading/
Overloaded Operators
주어진 operands(피연산자)의 종류에 따라 operator의 동작이 달라지는 것을 가리켜 overloaded opeartor라고 부름
[Python] overloading, overriding, and special methods
일반적인 Overloading overloading (or over-load, 과적?)이란 같은 이름의 function, method, operator를 여러 개로 중복 정의하는 것을 가르킴. function의 경우, call시 입력되는 arguments가 할당될 parameters를 다르게
ds31x.tistory.com
이 문서에서는 str에 대한 +, * 만 다루며, operator overloading을 하는 방법에 대해선 다음을 참고하라.
[Python] special methods and operator overloading
Special Methods사용자가 직접 호출하는 경우가 거의 없고, 간접적으로 호출이 됨.즉, 개발자(사용자)가 over-riding을 통해 구현은 하지만 직접 호출하는 경우가 거의 없고,개발자가 다른 built-in function
ds31x.tistory.com
Concatenation
+ opeartor combines its two operands.
다음과 같이, 두 문자열이 붙은 새로운 문자열을 반환한다.
>>> a = 'test1' + 'test2'
>>> a
'test1test1'
+ opeartor does not add any space between two operand strings to concate, contrast to the case of print method.
print 함수에서 argument 사이에 space가 포함되는 것과 달리 문자열 그대로 사용하여 결합만이 이루어짐.
Duplication (or Repetition, Replication)
곱하기가 아닌 앞의 operand 인 str이 뒤의 operand로 주어진 숫자만큼 반복되어 concatenation이 됨.
>>> 'one'*4
'oneoneoneone'
* operator has higher precedence thatn + operator.
사칙연산 들 중에서 문자열에 대해 overload가 이루어진 것은 위의 두가지임.
- and / are not supported for str
'Programming' 카테고리의 다른 글
| [PyQt6] QSizePolicy 설정. (0) | 2023.07.03 |
|---|---|
| [Python] Regular Expression : re 요약 (0) | 2023.07.03 |
| [Python] pip 란 (Package Management System) (0) | 2023.06.22 |
| [Python] Basic Methods of String (0) | 2023.06.21 |
| [Python] f-String (1) | 2023.06.21 |
