-
카테고리
-
세부 분야
프로그래밍 언어
-
해결 여부
미해결
Attribute Error
22.01.08 14:06 작성 조회수 369
0
AttributeError Traceback (most recent call last)
<ipython-input-2-060dc4245c24> in <module>() ----> 1 print(car1)
<ipython-input-1-cac9745f3ab9> in __str__(self) 57 58 def __str__(self): # 파이썬 내부에 이미 str인스턴스가 있음 (지원해줌) ---> 59 return 'str : {} - {}'.format(self._company. self.details) 60 61 car1 = car('Ferrari', {'color' : 'White', 'horsepower' : 400, 'price' : 8000})
AttributeError: 'str' object has no attribute 'self'
강의를 들으면서 코드 작성을 똑같이 했는데 Attribute Error가 뜨네요... 왜 str는 self를 받지 못한다고 나오는건지 모르겠습니다. 찾아보니 처음에 따로 데이터 형식을 지정해줘야 한다는 글도 보이는데 해봤는데 안되더라구요
답변을 작성해보세요.
0
좋은사람
지식공유자2022.01.09
return 'str : {} - {}'.format(self._company, self._details)
잘 살펴보시면 콤마로 구분하셔야 되여!! 점(.)이 찍혀 있어여!
0
0
안은정
질문자2022.01.08
class Car(): # 괄호는 없어도 됨
# str: 사용자 입장에서 출력할때는 (비공식적인, 프린터 문으로 문자열로 출력)
# repr: 객체 형태를 그대로 표시해줄때
def __init__(self, company, details):
self._company = company
self._details = details
def __str__(self): # 파이썬 내부에 이미 str인스턴스가 있음 (지원해줌)
return 'str : {} - {}'.format(self._company. self._details)
def __repr__(self):
return "repr : {} - {}".format(self._company. self._details)
car1 = Car('Ferrari', {'color' : 'White', 'horsepower' : 400, 'price' : 8000})
car2 = Car('BMW', {'color' : 'Black', 'horsepower' : 270, 'price' : 5000})
car3 = Car('Audi', {'color' : 'Shilver', 'horsepower' : 300, 'price' : 6000})
0
답변 4