[Python] str: Overloaded Operators

2023. 7. 2. 10:10·Programming
728x90
728x90
Operands의 Data type에 따라 연산자의 동작이 달라지는 대표적인 예임.
https://www.codingeek.com/tutorials/python/operator-overloading/

Overloaded Operators

주어진 operands(피연산자)의 종류에 따라 operator의 동작이 달라지는 것을 가리켜 overloaded opeartor라고 부름

https://ds31x.tistory.com/36

 

[Python] overloading, overriding, and special methods

일반적인 Overloading overloading (or over-load, 과적?)이란 같은 이름의 function, method, operator를 여러 개로 중복 정의하는 것을 가르킴. function의 경우, call시 입력되는 arguments가 할당될 parameters를 다르게

ds31x.tistory.com

이 문서에서는 str에 대한 +, * 만 다루며, operator overloading을 하는 방법에 대해선 다음을 참고하라.

https://ds31x.tistory.com/35

 

[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


 

728x90

'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
'Programming' 카테고리의 다른 글
  • [PyQt6] QSizePolicy 설정.
  • [Python] Regular Expression : re 요약
  • [Python] pip 란 (Package Management System)
  • [Python] Basic Methods of String
dsaint31x
dsaint31x
    반응형
    250x250
  • dsaint31x
    Dsaint31's blog
    dsaint31x
  • 전체
    오늘
    어제
    • 분류 전체보기 (787)
      • Private Life (15)
      • Programming (55)
        • DIP (116)
        • ML (35)
      • Computer (120)
        • CE (54)
        • ETC (33)
        • CUDA (3)
        • Blog, Markdown, Latex (4)
        • Linux (9)
      • ... (368)
        • Signals and Systems (115)
        • Math (176)
        • Linear Algebra (33)
        • Physics (43)
        • 인성세미나 (1)
      • 정리필요. (61)
        • 의료기기의 이해 (6)
        • PET, MRI and so on. (7)
        • PET Study 2009 (1)
        • 방사선 장해방호 (5)
        • 방사선 생물학 (3)
        • 방사선 계측 (9)
        • 기타 방사능관련 (3)
        • 고시 (9)
        • 정리 (18)
      • RI (0)
      • 원자력,방사능 관련법 (2)
  • 블로그 메뉴

    • Math
    • Programming
    • SS
    • DIP
  • 링크

    • Convex Optimization For All
  • 공지사항

    • Test
    • PET Study 2009
    • 기타 방사능관련.
  • 인기 글

  • 태그

    Python
    Term
    numpy
    random
    SIGNAL
    fourier transform
    ML
    Vector
    signals_and_systems
    SS
    Programming
    인허가제도
    signal_and_system
    cv2
    Optimization
    function
    linear algebra
    opencv
    math
    Probability
  • 최근 댓글

  • 최근 글

  • hELLO· Designed By정상우.v4.10.3
dsaint31x
[Python] str: Overloaded Operators
상단으로

티스토리툴바