kelerbs 2021-12-21 16:07 采纳率: 50%
浏览 56
已结题

python:为什么用.title方法会导致unsupported operand type(s) for +: 'builtin_function_or_method' and 'str'错误

#原代码如下:

class Restaurant():
    """模拟餐厅"""
    def __init__(self,name,type):
        """初始化属性name和age"""
        self.name = name
        self.type = type
    def describe_restaurant(self):
        """介绍restaurant的信息"""
        print("restaurant的名字是"+self.name.title()+",类型是"+self.type)
    def open_restaurant(self):
        """表示餐厅正在营业"""
        print(self.name.title+"正在营业中。")
my_restaurant = Restaurant("伏羲十星","十星级酒店")
my_restaurant.describe_restaurant()
my_restaurant.open_restaurant()

#结果报错:

Traceback (most recent call last):
  File "<stdin>", line 1, in <module>
  File "<stdin>", line 12, in open_restaurant
TypeError: unsupported operand type(s) for +: 'builtin_function_or_method' and 'str'

#修改1:在def open_restaurant(self)时,删除print函数中self.name.title的.title,结果运行成功

class Restaurant():
    """模拟餐厅"""
    def __init__(self,name,type):
        """初始化属性name和age"""
        self.name = name
        self.type = type
    def describe_restaurant(self):
        """介绍restaurant的信息"""
        print("restaurant的名字是"+self.name.title()+",类型是"+self.type)
    def open_restaurant(self):
        """表示餐厅正在营业"""
        print(self.name+"正在营业中。")
my_restaurant = Restaurant("伏羲十星","十星级酒店")
my_restaurant.describe_restaurant()
my_restaurant.open_restaurant()
>>> class Restaurant():
...     """模拟餐厅"""
...     def __init__(self,name,type):
...         """初始化属性name和age"""
...         self.name = name
...         self.type = type
...     def describe_restaurant(self):
...         """介绍restaurant的信息"""
...         print("restaurant的名字是"+self.name.title()+",类型是"+self.type)
...     def open_restaurant(self):
...         """表示餐厅正在营业"""
...         print(self.name+"正在营业中。")
...
>>> my_restaurant = Restaurant("伏羲十星","十星级酒店")
>>> my_restaurant.describe_restaurant()
restaurant的名字是伏羲十星,类型是十星级酒店
>>> my_restaurant.open_restaurant()
伏羲十星正在营业中。

#修改2:在def open_restaurant(self)时,用str函数强行把self.name.title换成string,结果失败

>>> class Restaurant():
...     """模拟餐厅"""
...     def __init__(self,name,type):
...         """初始化属性name和age"""
...         self.name = name
...         self.type = type
...     def describe_restaurant(self):
...         """介绍restaurant的信息"""
...         print("restaurant的名字是"+self.name.title()+",类型是"+self.type)
...     def open_restaurant(self):
...         """表示餐厅正在营业"""
...         print(str(self.name.title)+"正在营业中。")
...
>>> my_restaurant = Restaurant("伏羲十星","十星级酒店")
>>> my_restaurant.describe_restaurant()
restaurant的名字是伏羲十星,类型是十星级酒店
>>> my_restaurant.open_restaurant()
<built-in method title of str object at 0x000001FBB2493690>正在营业中。

我想请问一下,为什么用self.name.title会出错,以及同样是self.name.title,同样是print函数,describe_restaurant(self)就不受影响,而open_restaurant(self)就会出错?多谢啦~

  • 写回答

3条回答 默认 最新

  • CSDN专家-sinJack 2021-12-21 16:22
    关注
    class Restaurant():
        """模拟餐厅"""
        def __init__(self,name,type):
            """初始化属性name和age"""
            self.name = name
            self.type = type
        def describe_restaurant(self):
            """介绍restaurant的信息"""
            print("restaurant的名字是"+self.name.title()+",类型是"+self.type)
        def open_restaurant(self):
            """表示餐厅正在营业"""
            print(self.name.title()+"正在营业中。")
    my_restaurant = Restaurant("伏羲十星","十星级酒店")
    my_restaurant.describe_restaurant()
    my_restaurant.open_restaurant()
    

    img

    本回答被题主选为最佳回答 , 对您是否有帮助呢?
    评论 编辑记录
查看更多回答(2条)

报告相同问题?

问题事件

  • 系统已结题 12月29日
  • 已采纳回答 12月21日
  • 创建了问题 12月21日

悬赏问题

  • ¥15 RPA正常跑,cmd输入cookies跑不出来
  • ¥15 求帮我调试一下freefem代码
  • ¥15 matlab代码解决,怎么运行
  • ¥15 R语言Rstudio突然无法启动
  • ¥15 关于#matlab#的问题:提取2个图像的变量作为另外一个图像像元的移动量,计算新的位置创建新的图像并提取第二个图像的变量到新的图像
  • ¥15 改算法,照着压缩包里边,参考其他代码封装的格式 写到main函数里
  • ¥15 用windows做服务的同志有吗
  • ¥60 求一个简单的网页(标签-安全|关键词-上传)
  • ¥35 lstm时间序列共享单车预测,loss值优化,参数优化算法
  • ¥15 Python中的request,如何使用ssr节点,通过代理requests网页。本人在泰国,需要用大陆ip才能玩网页游戏,合法合规。