Welcome to MLink Developer Q&A Community for programmer and developer-Open, Learning and Share
Welcome To Ask or Share your Answers For Others

Categories

0 votes
450 views
in Technique[技术] by (71.8m points)

python - "'str' object is not callable". Is it something simple I'm missing?

I am receiving, this error please help:

Traceback (most recent call last):
  File ".class practice2.py", line 18, in <module>
    kitty.meow('jelly')
TypeError: 'str' object is not callable
class Cat():


    def __init__(self,breed,color,meow,name):
        self.breed = breed
        self.color = color
        self.meow = meow
        self.name = name


    def meow(self,snack):
        print(f"MEOW, My name is {self.name} and my favorite snack is {snack}")


kitty = Cat('Maine Coon', 'White and black','ugly','kitty')

cat = Cat('Unknown','Grey', 'Ugly', 'cat')

kitty.meow('jelly')

cat.meow('meat')

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome To Ask or Share your Answers For Others

1 Answer

0 votes
by (71.8m points)

You have two meow meaning two separate things in your code. It conflicts. Change either

def meow(self,snack):

To something else. like

def meowFunction(self,snack):

Or change

def __init__(self,breed,color,meow,name):
        self.breed = breed
        self.color = color
        self.meow = meow
        self.name = name

To something else like:

def __init__(self,breed,color,catSound,name):
        self.breed = breed
        self.color = color
        self.catSound = catSound
        self.name = name

Two things should not be named the same thing. The compiler/interpreter will get confused and error out.


与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome to MLink Developer Q&A Community for programmer and developer-Open, Learning and Share

1.2m questions

2.1m answers

5 comments

56.5k users

...