python error L “TypeError: __str__ returned non-string” but still prints to output?

2020. 3. 7. 18:28·Error code 모음/4. Python errors
반응형

__str__ 는 특수 메서드 중 하나입니다.

 

class Element():
    def __init__(self, name, symbol, number):
        self.name = name
        self.symbol = symbol
        self.number = number
        
    def __str__(self):
        print("name:{0}    symbole:{1}    number:{2}".format(self.name, self.symbol, self.number))

특수 메서드는 print 가 아닌 return  으로 값을 받아줘야 에러없이 출력이 되는데

위 에어는 print( ) 로 값을 받아서 생긴 에러였네요

 

print( ) --> return 으로 해야합니다.

class Element():
    def __init__(self, name, symbol, number):
        self.name = name
        self.symbol = symbol
        self.number = number
        
    def __str__(self):
        return "name:{0}    symbole:{1}    number:{2}".format(self.name, self.symbol, self.number)

 

아래 stakoverflow 에서 이유를 확인할 수 있습니다.

https://stackoverflow.com/questions/11871221/python-typeerror-str-returned-non-string-but-still-prints-to-output

 

Python: "TypeError: __str__ returned non-string" but still prints to output?

I have this piece of code which creates a new note..WHen I try to print I get the following error even though it prints the output Error: C:\Python27\Basics\OOP\formytesting>python notebook.py ...

stackoverflow.com

특수 메서드에 대해서 확인하려면 아래 링크를 참고해주세요

https://wikidocs.net/89

 

위키독스

온라인 책을 제작 공유하는 플랫폼 서비스

wikidocs.net

 

반응형

'Error code 모음 > 4. Python errors' 카테고리의 다른 글

python error: do_this( ) takes 1 positional argument but 15 were given  (0) 2020.03.18
python error: The "freeze_support()" line can be omitted if the program is not going to be frozen to produce an executable.  (0) 2020.03.17
python error : __init__() missing 1 required positional argument:  (0) 2020.03.07
파이썬 에러 : 'tuple' object has no attribute 'get'  (0) 2020.03.04
python error : "<stdin>", line1, in <module> IndexError: list index out of range  (0) 2020.02.29
'Error code 모음/4. Python errors' 카테고리의 다른 글
  • python error: do_this( ) takes 1 positional argument but 15 were given
  • python error: The "freeze_support()" line can be omitted if the program is not going to be frozen to produce an executable.
  • python error : __init__() missing 1 required positional argument:
  • 파이썬 에러 : 'tuple' object has no attribute 'get'
쟈누
쟈누
Ad astra per aspera
    반응형
  • 쟈누
    쟈누의 기록공간
    쟈누
  • 전체
    오늘
    어제
    • 분류 전체보기 (444)
      • AWS (31)
        • Glue (4)
        • S3 (1)
      • 클라우드 (0)
      • Data Engineering (37)
        • GitHub (10)
        • NiFi (11)
        • Spark (10)
        • Snowflake (0)
        • 머신러닝, AI (6)
      • 언어 (118)
        • 데이터 베이스 (42)
        • JAVA (9)
        • Python (34)
        • Java Script (15)
        • Linux (18)
      • 프로젝트, 인강 그리고 책 (30)
        • Spotify Project (7)
        • RASA chatbot Project (9)
        • Naver shopping Project (6)
        • 빅데이터를 지탱하는 기술 (8)
      • OLD (56)
        • IT 용어 사전 (13)
        • Front End (12)
        • Back End (31)
      • Error code 모음 (165)
        • 1. SQL errors (17)
        • 2. Hadoop errors (20)
        • 3. Linux Errors (14)
        • 4. Python errors (33)
        • 5. JAVA, Spring errors (41)
        • 6. Jav Script errors (10)
        • 7. Dev Tools errors (9)
        • 8. Git errors (8)
        • 9. Jenkins Errors (4)
        • 10. airflow Errors (2)
        • 11. Aws errors (7)
      • 개인 (1)
        • 책 (1)
  • 블로그 메뉴

    • 홈
    • 태그
    • 방명록
    • 블로그 관리
    • 글쓰기
  • 링크

  • 공지사항

    • 간단한 블로그 소개
  • 인기 글

  • 태그

    Spring
    install
    SQL
    java
    python error
    에러
    리눅스
    json
    error
    AWS
    파이썬
    Python
    API
    Git
    자바
    node
    linux
    MySQL
    설치
    NiFi
  • 최근 댓글

  • 최근 글

  • hELLO· Designed By정상우.v4.10.3
쟈누
python error L “TypeError: __str__ returned non-string” but still prints to output?
상단으로

티스토리툴바