为什么类的方法在定义的时候总要加入self,没写也不会报错,程序也没有问题啊?
Topic sourceimport functools def log(func): if type(func)==type(lambda x:xx): def decorator(args, kwargs): print('call %s' % func.name) return func(args, kwargs) return decorator else: text=func def decorator(func1): def wrapper(args, *kw): print('%s %s():' % (text, func1.name)) return func1(args, kw) return wrapper return decorator
@log def now(): return ('2015-3-25') print(now())
@log('excetion') def now(): return ('2015-3-25') print(now())
不写肯定不一样的,self可以理解为类的初始化实例,缺少了这个self你就没有办法进行实例化调用方法
__author__='lvxinsjtu'
class a():
def b():
print('yes')
c=a()
c.b()
Traceback (most recent call last):
File "<input>", line 1, in <module>
TypeError: b() takes 0 positional arguments but 1 was given
在此插入代码
而你说的a.b()的调用实际上并没有对类进行实例化
- 1
脑洞开天
为什么类的方法在定义的时候总要加入self,没写也不会报错,程序执行也没有问题啊? 不写系统会默认添加吗?