Python setter TypeError : 'int'개체를 호출 할 수 없습니다.

Muten_Roshi

내 개인 self .__ food 변수에 대한 setter를 만들려고합니다. 기본적으로 나는 자식 클래스 Tiger가 값을 100 이상으로 제한하는 조건이있는 개인 변수를 변경하기를 원합니다. 그러나 오류가 발생합니다. TypeError : 'int'object is not callable

내가 어디에서 잘못되었으며 어떻게 해결할 수 있습니까? 감사

class Animal:
    def __init__(self,foodamount=10, location = 'Australia'):
        self.__food = foodamount
        self.location = location

    @property
    def foodamt(self):
        return self.__food

    @foodamt.setter
    def foodsetter(self, foodamount):
        if self.__food >100:
            self.__food = 100
        else: self.__food = foodamount


class Tiger(Animal):
    def __init__(self,colour = 'orange'):
        super().__init__(location ='programming and gaming')
        self.colour = colour


an = Tiger()
an.colour='red'
print(an.colour)
ansetfood = an.foodsetter(1000)
print(ansetfood)
케빈

몇 가지 문제가 있습니다.

  • 속성을 사용할 때와 같은 setter의 이름을 수동으로 호출하지 마십시오 an.foodsetter(1000). 와 같은 속성 할당 구문을 사용합니다 an.foodamt = 1000. 이것이 속성의 전체 요점입니다. 함수와 같은 동작을 유지하면서 투명한 속성과 같은 구문을 갖는 것입니다.
  • .이 foodamount아니라 100 과 비교해야합니다 self.__food.
  • 속성의 getter 및 setter는 동일한 이름을 가져야합니다.
class Animal:
    def __init__(self,foodamount=10, location = 'Australia'):
        self.__food = foodamount
        self.location = location

    @property
    def foodamt(self):
        return self.__food

    @foodamt.setter
    def foodamt(self, foodamount):
        if foodamount >100:
            self.__food = 100
        else: self.__food = foodamount


class Tiger(Animal):
    def __init__(self,colour = 'orange'):
        super().__init__(location ='programming and gaming')
        self.colour = colour


an = Animal()
an.colour='red'
print(an.colour)
an.foodamt = 1000
print(an.foodamt)

결과:

red
100

이 기사는 인터넷에서 수집됩니다. 재 인쇄 할 때 출처를 알려주십시오.

침해가 발생한 경우 연락 주시기 바랍니다[email protected] 삭제

에서 수정
0

몇 마디 만하겠습니다

0리뷰
로그인참여 후 검토

관련 기사

분류에서Dev

Python Script TypeError : 'int'개체를 호출 할 수 없습니다.

분류에서Dev

TypeError : 'Int64Index'개체를 호출 할 수 없습니다.

분류에서Dev

TypeError : 'int'객체는 Python 3을 호출 할 수 없습니다.

분류에서Dev

TypeError : 'int'객체는 Python으로 호출 할 수 없습니다.

분류에서Dev

Python Tkinter TypeError : 'int'객체는 호출 할 수 없습니다.

분류에서Dev

Python TypeError : 'int'개체는 OOP에서 호출 할 수 없습니다.

분류에서Dev

Python 오류 : TypeError : 'list'개체를 호출 할 수 없습니다.

분류에서Dev

Python / Pygame : TypeError : '모듈'개체를 호출 할 수 없습니다.

분류에서Dev

Python-TypeError : 'list'개체를 호출 할 수 없습니다.

분류에서Dev

Python TypeError : '모듈'개체를 호출 할 수 없습니다.

분류에서Dev

Python 표시 :-TypeError : 코드를 실행하는 동안 Python에서 'int'개체를 호출 할 수 없습니다.

분류에서Dev

Python 3.x에서 오류 발생 : TypeError : 'int'개체를 호출 할 수 없습니다.

분류에서Dev

Python timeit-TypeError : '모듈'개체를 호출 할 수 없습니다.

분류에서Dev

Python 클래스 : TypeError : 'NoneType'개체를 호출 할 수 없습니다.

분류에서Dev

Python 모듈 문제 : TypeError : '모듈'개체를 호출 할 수 없습니다.

분류에서Dev

TypeError : 'int'객체는 호출 할 수 없습니다 .``, len ()

분류에서Dev

시리즈를 호출 할 때 Python, Pandas 및 NLTK 유형 오류 'int'개체를 호출 할 수 없습니다.

분류에서Dev

TypeError : '_IncompatibleKeys'개체를 호출 할 수 없습니다.

분류에서Dev

TypeError : 'NoneType'개체는 CircleCI를 호출 할 수 없습니다.

분류에서Dev

TypeError : 'CurrencyConverter'개체를 호출 할 수 없습니다.

분류에서Dev

firebase = firebase (config) TypeError : 'module'개체를 호출 할 수 없습니다.

분류에서Dev

TypeError : '목록'개체를 호출 할 수 없습니다-assertWarns ()

분류에서Dev

TypeError : '목록'개체를 호출 할 수 없습니다-assertWarns ()

분류에서Dev

Q : TypeError : '_csv.reader'개체를 호출 할 수 없습니다.

분류에서Dev

TypeError : 'Player'개체는 Django를 호출 할 수 없습니다.

분류에서Dev

TypeError : 'Post'개체를 호출 할 수 없습니다.

분류에서Dev

groupby-TypeError 'DataFrame'개체를 호출 할 수 없습니다.

분류에서Dev

TypeError : '게임'개체를 호출 할 수 없습니다.

분류에서Dev

TypeError : 'Weather'개체를 호출 할 수 없습니다.

Related 관련 기사

  1. 1

    Python Script TypeError : 'int'개체를 호출 할 수 없습니다.

  2. 2

    TypeError : 'Int64Index'개체를 호출 할 수 없습니다.

  3. 3

    TypeError : 'int'객체는 Python 3을 호출 할 수 없습니다.

  4. 4

    TypeError : 'int'객체는 Python으로 호출 할 수 없습니다.

  5. 5

    Python Tkinter TypeError : 'int'객체는 호출 할 수 없습니다.

  6. 6

    Python TypeError : 'int'개체는 OOP에서 호출 할 수 없습니다.

  7. 7

    Python 오류 : TypeError : 'list'개체를 호출 할 수 없습니다.

  8. 8

    Python / Pygame : TypeError : '모듈'개체를 호출 할 수 없습니다.

  9. 9

    Python-TypeError : 'list'개체를 호출 할 수 없습니다.

  10. 10

    Python TypeError : '모듈'개체를 호출 할 수 없습니다.

  11. 11

    Python 표시 :-TypeError : 코드를 실행하는 동안 Python에서 'int'개체를 호출 할 수 없습니다.

  12. 12

    Python 3.x에서 오류 발생 : TypeError : 'int'개체를 호출 할 수 없습니다.

  13. 13

    Python timeit-TypeError : '모듈'개체를 호출 할 수 없습니다.

  14. 14

    Python 클래스 : TypeError : 'NoneType'개체를 호출 할 수 없습니다.

  15. 15

    Python 모듈 문제 : TypeError : '모듈'개체를 호출 할 수 없습니다.

  16. 16

    TypeError : 'int'객체는 호출 할 수 없습니다 .``, len ()

  17. 17

    시리즈를 호출 할 때 Python, Pandas 및 NLTK 유형 오류 'int'개체를 호출 할 수 없습니다.

  18. 18

    TypeError : '_IncompatibleKeys'개체를 호출 할 수 없습니다.

  19. 19

    TypeError : 'NoneType'개체는 CircleCI를 호출 할 수 없습니다.

  20. 20

    TypeError : 'CurrencyConverter'개체를 호출 할 수 없습니다.

  21. 21

    firebase = firebase (config) TypeError : 'module'개체를 호출 할 수 없습니다.

  22. 22

    TypeError : '목록'개체를 호출 할 수 없습니다-assertWarns ()

  23. 23

    TypeError : '목록'개체를 호출 할 수 없습니다-assertWarns ()

  24. 24

    Q : TypeError : '_csv.reader'개체를 호출 할 수 없습니다.

  25. 25

    TypeError : 'Player'개체는 Django를 호출 할 수 없습니다.

  26. 26

    TypeError : 'Post'개체를 호출 할 수 없습니다.

  27. 27

    groupby-TypeError 'DataFrame'개체를 호출 할 수 없습니다.

  28. 28

    TypeError : '게임'개체를 호출 할 수 없습니다.

  29. 29

    TypeError : 'Weather'개체를 호출 할 수 없습니다.

뜨겁다태그

보관