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

Categories

0 votes
437 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')
question from:https://stackoverflow.com/questions/65516721/str-object-is-not-callable-is-it-something-simple-im-missing

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

1 Reply

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.


与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
OGeek|极客中国-欢迎来到极客的世界,一个免费开放的程序员编程交流平台!开放,进步,分享!让技术改变生活,让极客改变未来! Welcome to OGeek Q&A Community for programmer and developer-Open, Learning and Share
Click Here to Ask a Question

1.4m articles

1.4m replys

5 comments

57.0k users

...