Thursday, August 13, 2009

trac源代码阅读 - 6 python operators

结果在这里: http://docs.python.org/library/operator.html
知道这些, 我们就可以像c那样重载操作符了
这应该也算是descriptor的一种吧

Wednesday, August 05, 2009

trac源代码阅读 - 5 python operators

__add__ == +
>>> class a(object):
... def __or__(self, t):
... return id(t)
... def __add__(self, t):
... return id(t)
...
>>> c = a()
>>> a()+c
13041552
>>> id(c)
13041552




__sub__ == -
>>> class a(object):
... def __or__(self, t):
... return id(t)
... def __add__(self, t):
... return id(t)
... def __sub__(self, t):
... return id(t)
...
>>> c = a()
>>> a()-c
13041552
>>> id(c)
13041552

why? will continue in next post.

trac源代码阅读 - 4 ("|" or operator)

__or__ == |

Error:
>>> a={'a':1, }
>>> b = lambda x: x*2
>>> a | b
Traceback (most recent call last):
  File "<stdin>", line 1, in <module>
TypeError: unsupported operand type(s) for |: 'dict' and 'function'

the first object must has "__or__" method

Right:
>>> class a(object):
...     def __or__(self, t):
...             return id(t)
...
>>> c = a()
>>> d = a()
>>> c|d
13041232
>>> id(c)
13041264
>>> id(d)
13041232
>>> c|""
12492856

Example: (Transformer)

Tuesday, August 04, 2009

innerHTML中, p包含ul的问题

一个dom节点给他innerHTML="<p class='temp'><ul><li>test</li></ul></p>", 其中temp的样式为: .temp {display:none;}, 此时ul元素会被解析到p标签之外, IE, FF, Chrome都是如此, BUG or Standard?