You need to enable JavaScript to run this app.
优惠活动
大模型
产品
解决方案
定价
更多

Python类调用内部方法报NameError,求实现指定文本结构方案

解决Python类中_my_base方法调用的NameError问题

问题原因

代码报错是因为类内方法的调用方式错误:

  • _my_base、first_text、second_text都没有正确声明为实例方法或类方法,导致在first_text中直接调用_my_base时,Python找不到这个名称。
  • 你直接用my_test = my_class调用方法,本质是调用类方法,但原代码里的方法没有对应声明。

解决方案1:使用实例方法

给每个方法添加self参数(代表类实例),通过self._my_base调用基础方法,调用时需要先创建类实例:

class my_class:
    def _my_base(self, my_def_text, title, description):
        rtn = ['this is a test',
               '',
               'some text',
               f'      {title}',
               '      this is some text that is needed',
               f'      {description}',
               f'{my_def_text}'
               ]
        return "\n".join(rtn)
    
    def first_text(self, value):
        title = 'First text'
        description = "this is a test"
        rtn = ['red',
               '',
               'green',
               'blue',
               f'      {value}',
               ]
        return self._my_base("\n".join(rtn), title, description)
    
    def second_text(self, value):
        title = 'second text'
        description = "this is a test"
        rtn = ['      some text',
               f'      {value}',
               '',
               'This is to show that I want the text in different places',
               ]
        return self._my_base("\n".join(rtn), title, description)

调用示例:

>>> my_test = my_class()
>>> print(my_test.first_text("hello"))

解决方案2:使用类方法(无需创建实例)

用@classmethod装饰器声明方法,第一个参数改为cls(代表类本身),通过cls._my_base调用,直接用类或类引用调用方法:

class my_class:
    @classmethod
    def _my_base(cls, my_def_text, title, description):
        rtn = ['this is a test',
               '',
               'some text',
               f'      {title}',
               '      this is some text that is needed',
               f'      {description}',
               f'{my_def_text}'
               ]
        return "\n".join(rtn)
    
    @classmethod
    def first_text(cls, value):
        title = 'First text'
        description = "this is a test"
        rtn = ['red',
               '',
               'green',
               'blue',
               f'      {value}',
               ]
        return cls._my_base("\n".join(rtn), title, description)
    
    @classmethod
    def second_text(cls, value):
        title = 'second text'
        description = "this is a test"
        rtn = ['      some text',
               f'      {value}',
               '',
               'This is to show that I want the text in different places',
               ]
        return cls._my_base("\n".join(rtn), title, description)

调用示例:

>>> my_test = my_class
>>> print(my_test.first_text("hello"))
# 或直接调用:my_class.first_text("hello")

说明

  • 实例方法适合需要维护实例状态的场景,必须通过类实例调用。
  • 类方法适合无需实例状态的工具类场景,直接通过类调用即可。

内容的提问来源于stack exchange,提问作者Klint Krossa

相关产品推荐
方舟 Agent Plan

超全模态模型 × Harness 升级,最新支持 Deepseek-V4.1-Flash、GLM-5.3 系列、Doubao-Seedream-5.0-pro、Kimi-K3 (部分), 限时 9.9 元起

最近更新时间:2026.07.21 05:27:05