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
相关文档:
用python写的抓取天气预报的脚本
http://blog.chinaunix.net/u2/82009/showart_2166843.html
从昨天开始的看关于网络抓取的东西,而且自己的用的是awesome ,所以写了这个天气预报的脚本给我的awesome,这个天气脚本直接取下来的话是七天的天气预报从中国天气网上,我后面对它做了处理,用到了我的awesome上
效果:1日星 ......
python 的内嵌time模板翻译及说明
一、简介
time模块提供各种操作时间的函数
说明:一般有两种表示时间的方式:
第一种是时间戳的方式(相对于1970.1.1 00:00:00以秒计算的偏移量),时间戳是惟一的
第二种以数组的形式表示即(struct_time),共有九个元素,分别表示,同一个时间戳的struct_time会因为时区不同而不同
year ......
python string和PyQt的QString的区别 以下在Python2.6和PyQt4.4.4 for
Python2,6环境下讨论: Python中有两种有关字符的类型:Python string object和Python Unicode
object。主要使用Python string object进行数据输入输出。 PyQt中与之相对应的字符有关类
python string和PyQt的QString的区别
以下在Python2.6和PyQt4 ......
来源:
作者:
灵剑
1.python 字符串通常有单引号('...')、双引号(...)、三引号(...)或('''...''')包围,三引号包含的字符串可由多行组成,一般可表示大段的叙述性字符串。在使用时基本没有差别,
1.python
字符串通常有单引号('...')、双引号("...")、三引号("""... ......
背景
项目的
自动化测试中已经使用了基于Python
脚本的框架,自动化过程中最关键的问题就是如何实现桩模块。运用
Python
强大的功能,实现任何桩模块都是可能的,但是是否必须完全使用
Python
实现模块逻辑,成本是一个决定性因素。在桩模块逻辑简单的情况下,使用
Python
模拟模块逻辑不但使自动化测试的结构清 ......