鍍金池/ 問答/Python/ [初學(xué)] return int類型報(bào)錯(cuò) TypeError: 'int' obj

[初學(xué)] return int類型報(bào)錯(cuò) TypeError: 'int' object is not callable

請(qǐng)教為什么如下編寫 return 一個(gè) int 會(huì)報(bào)錯(cuò):

TypeError: 'int' object is not callable

而用 lambda 就可以呢?

class Student(object):
    def __getattr__(self, attr):
        if attr == 'age':
            return 25
class Student(object):
    def __getattr__(self, attr):
        if attr == 'age':
            return lambda: 25
回答
編輯回答
巷尾
student = Student()

# 上面一種
student.age    # 返回 25
student.age()  # 25是數(shù)字不是函數(shù),不能執(zhí)行,報(bào)錯(cuò)

# 下面一種
student.age    # 返回匿名函數(shù)
student.age()  #  執(zhí)行這個(gè)匿名函數(shù),返回25
2017年1月19日 19:04