Python TypeError: 'NoneType' object is not callable

本文探讨了Python中没有内置Switch Case语句的问题,并提供了一种使用字典来模仿Switch Case的解决方案。作者在尝试实现一个命令行计算器时遇到了错误,通过调整字典中的方法引用而非调用,成功解决了TypeError。

摘要生成于 C知道 ,由 DeepSeek-R1 满血版支持, 前往体验 >

 

I am mere a beginner of Python. I am getting the following error and suspect that it has something to do with the dictionary I've used as switch case (since python does not provide switch ). Following is my code:

class Arithmetic:

    a,b,choice = 0,0,0


    def __init__(self):

        print "\n\n"

        for num in range(30):
            print "*",

        print "\n"    

        print "Welcome to CLC (Command Line Calculator)"

        print "\n"

        for num in range(30):
            print "*",

        print "\n"

    def menu(self):

        print "1. Add"
        print "2. Substract"
        print "3. Multiply"
        print "4. Divide"
        print "5. Modulo"
        print "6. Exit \n\n"

        self.choice = raw_input("Enter Your Choice: ")

        if self.choice == '0':
            exit("Thank you for using the program")

        selector = {
                "1" :   self.add(),
                "2" :   self.substract(), 
                "3" :   self.multiply(),
                "4" :   self.divide(),
                "5" :   self.modulo()
                }

        selector[self.choice]()                    

    def add(self):
        print "Add called"

    def substract(self):
        print "Substract called"

    def multiply(self):
        print "Multiply called"

    def divide(self):
        print "Divide called"

    def modulo(self):
        print "Modulo called"


    def main(self):

        while self.choice != '6':
            self.menu()   



a = Arithmetic()
a.menu() 
Traceback (most recent call last):
 File "arithmetics.py", line 75, in <module>
a.menu()
File "arithmetics.py", line 43, in menu
  selector[self.choice]()                    
TypeError: 'NoneType' object is not callable

The Error is:

When you do

self.add()

you are calling the method (you will get a result). If you want to specify the method, delete the ():

selector = {
            "1" :   self.add,
            "2" :   self.substract, 
            "3" :   self.multiply,
            "4" :   self.divide,
            "5" :   self.modulo
            }
### 解决 Python 中 `TypeError: 'NoneType' object is not callable` 错误 当遇到 `TypeError: 'NoneType' object is not callable` 的错误时,通常是因为程序试图调用一个本应为可调用对象(如函数或类实例),但却意外地变成了 `None` 值。这种情况下,可以采取以下措施来排查和解决问题。 #### 1. 检查变量赋值逻辑 确保所有预期作为函数使用的变量确实被赋予了一个有效的函数引用而不是 `None`。如果某个地方可能返回 `None` 或未定义,则应在使用前进行验证[^1]。 ```python def safe_call(func, *args, **kwargs): if func is not None and callable(func): return func(*args, **kwargs) else: raise ValueError("Function reference cannot be None or non-callable.") ``` #### 2. 审视装饰器应用 有时,在使用自定义或第三方库中的装饰器时可能会引发此异常。这可能是由于装饰器本身存在问题或是其与目标函数之间的兼容性不佳所致。务必确认所使用的装饰器不会无意间将原本正常的函数替换成了 `None`。 #### 3. 更新IDE及其扩展插件 考虑到开发工具链的影响,特别是像 VSCode 这样的集成开发环境 (IDE),旧版 IDE 及其自动更新但不匹配的 Python 插件也可能成为问题根源之一。建议保持这些组件处于最新状态以减少潜在冲突[^2]。 #### 4. 调试代码路径 通过添加日志记录或其他调试手段跟踪代码执行流程,找出具体在哪一步出现了期望之外的结果——即某处应该是一个有效的方法/函数却被设置为了 `None`。这样做有助于精确定位问题所在位置并加以修正[^3]。
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值