example of python operator overloadind
And last here is the overload operators example:
# map() takes two (or more) arguments, a function and a list to apply the function to
# lambda can be put anywhere a function is expected
# map() calls lambada for every element in the self list
# since Vector has overloaded __getitem__ and __len__ definitions
# the Vector object can be considered a list
# the lambda function adds each other item to each item in the list
# note this only adds objects that can typicaly be added by python
# print statements added to show what is getting called
class Vector:
def __init__(self, data):
print "__init__"
self.data = data
def __call__(self, varA, varB):
print "__call__"
print "do something with ", varA, " and ", varB
# overload print
# repr returns a string containing a printable representation of an object
# otherwise printing a Vector object would look like:
#<__main__.Vector instance at 0x0000000017A9DF48>
def __repr__(self):
print "__repr__"
return repr(self.data)
# overload +
def __add__(self, other):
print "__add__"
return Vector(map(lambda x, y: x+y, self, other))
# overload -
def __sub__(self, other):
print "__sub__"
return Vector(map(lambda x, y: x-y, self, other))
# overload /
def __div__(self, other):
print "__div__"
return Vector(map(lambda x, y: x/y, self, other))
# overload *
def __mul__(self, other):
print "__mul__"
return Vector(map(lambda x, y: x*y, self, other))
# overload %
def __mod__(self, other):
print "__mod__"
return Vector(map(lambda x, y: x%y, self, other))
# overload []
def __getitem__(self, index):
print "__getitem__"
return self.data[index]
# overload set []
def __setitem__(self, key, item):
print "__setitem__"
self.data[key] = item
# return size to len()
def __len__(self):
print "__len__"
retur
相关文档:
如何写一个返回多个值的函数
函数的return 语句只能返回一个值,可以是任何类型。
因此,我们可以“返回一个 tuple类型,来间接达到返回多个值
”。
例: x 除以 y 的余数与商的函数
def F1 ( x, y ):
a = x % y
  ......
Programming Python, 2nd Edition (O'Reilly)
http://www.osbbs.com/dl/Programming Python, 2nd Edition (O'Reilly).chm
很全很经典了python学习入门资料
OReilly - Learning Python:
http://www.osbbs.com/dl/OReilly - Learning Python.chm
......
实验环境:windows xp + vim
文件:test.py。编码:ansi
我们的目标操作test.py中保存的非英文字母。
文件头的#encoding=utf8/gbk,这个是用来说明源文件的硬盘编码以便python识别[4]。
----------------------------------------------
输入:
x = '中文'
输出: 编译失败
编译时需要知道‘中文’的硬盘编 ......
Python的内存泄漏及gc模块的使用
-- 6.11日错误修正版
Horin|贺勤
Email: horin153@msn.com
......
我们在做软件开发的时候很多要用到多线程技术。例如如果做一个下载软件象flashget就要用到、象在线视频工具realplayer也要用到因为要同时下载media stream还要播放。其实例子是很多的。
线程相对进程来说是“轻量级”的,操作系统用较少的资源创建和管理线程。程序中的线程在相同的内存空间中执行,并共享许多 ......