self-confidence,the source of all the power

导航

2011年4月29日 #

python __call__ 函数

摘要: __call__ Python中有一个有趣的语法,只要定义类型的时候,实现__call__函数,这个类型就成为可调用的。换句话说,我们可以把这个类型的对象当作函数来使用,相当于 重载了括号运算符。class g_dpm(object):def __init__(self, g):self.g = gdef __call__(self, t):return (self.g*t**2)/2计算地球场景的时候,我们就可以令e_dpm = g_dpm(9.8),s = e_dpm(t)。class Animal(object): def __init__(self, name, legs): self 阅读全文

posted @ 2011-04-29 15:56 漩涡鸣人 阅读(29885) 评论(0) 推荐(2)

a few python special functions:__getitem__

摘要: 1、__getitem__原文文档:For instance, if a class defines a method named__getitem__(), andxis an instance of this class, thenx[i]is roughly equivalent tox.__getitem__(i)for old-style classes andtype(x).__getitem__(x,i)for new-style classes. Except where mentioned, attempts to execute an operation raise an 阅读全文

posted @ 2011-04-29 10:24 漩涡鸣人 阅读(1971) 评论(0) 推荐(0)