2017-01-22 1 views
-2
class Dog(object): 
    species = "mammal" 
    def __init__(self, breed, name, color): 
     self.breed = breed 
     self.name = name 

sam = Dog(breed="lab", name="maorian", color="white") 

그러나 "color"속성을 사용할 수 없습니다. 이유가 무엇입니까?Python 클래스의 다중 속성

답변

1

이니셜 라이저에서 설정하지 않았습니다. self.name = name 줄 다음에 self.color = color을 추가하십시오.

+0

감사합니다. 그러나 내가 100 가지 속성을 말하면, 이것은 너무 지저분 해지지 않습니까? –

+0

@ Nirupbelongokar 백가지 속성이 있다면 코드 냄새가 난다. https://sourcemaking.com/refactoring/smells/large-class – jonrsharpe