2005年06月08日 星期三 14:29
一、
>>> a = range(10)
>>> a
[0, 1, 2, 3, 4, 5, 6, 7, 8, 9]
>>> for i in a:
i += 1
>>> a
[0, 1, 2, 3, 4, 5, 6, 7, 8, 9]
如果a中元素是数,用for就不能改变a中元素,只能用filter,map,reduce作复杂
处理了,如果a中元素是list,就可以。不统一。
二、
a = "abccdd"
a[0] = 'c"
出错,想改a中的一个元素就要再生成一个新的string,这多慢
三、
函数默认参数是list的话,每次调用都用同一个list,会发生错误。
>>> def foo( a = []):
a.append(10)
print a
>>> foo()
[10]
>>> foo()
[10, 10]
>>>
这个问题影响不大,可为什么不改掉,很容易产生错误
2005年06月08日 星期三 14:30
Qiangning Hong,您好!
哦,不好意思,我没有描述清楚,我的cgi程序是在自己的机器上的, 代码中的"www.musi-cal.com/cgi-bin/query?"只是一个例子(python帮助里面的例子),实际上我转向的是一个我本地
其他一个web服务器上的某个页面,这个页面包含有图片. 我描述清楚:
web服务器使用python里面带的CGIHttpServer.py.
浏览器请求"http://localhost:8000/cgi-bin/test.py", test.py的代码是:
import cgitb
import urllib
cgitb.enable()
print "Content-Type: text/html" # HTML is following
print # blank line, end of headers
f = urllib.urlopen('http://www.sina.com.cn')
print f.read()
这样浏览器得到的是www.sina.com.cn的网页,但是很多图片显示不出来
我想实现的效果就是,把浏览器的请求重定向到www.sina.com.cn, 就跟浏览器直接请求新浪的网页一样. 而不是只是将urlopen后新浪页面的html代码显示出来.
因此可能不是使用urlopen,但是我不知道其他什么方法可以实现.
===== 2005-06-08 13:43:04 您在来信中写道:=======
>陈锦 wrote:
>> Qiangning Hong,您好!
>>
>> 对,是使用windows2000平台
>> 我按照你的方法设置为bin模式后,输出结果还是一样,无法显示图片
>>
>> 补充说明一下:
>> 使用urllib.urlopen, 只是显示urllib.urlopen返回的html代码,并没有进行url重定向. 因为浏览栏里面的地址并没有被改为http://www.musi-cal.com... , 所以可能是使用其他的方法来完成重定向的, 请高手们指教啊.
>
>我试了一下你的代码,返回的html代码里没有
啊?你要显示什么图片?
>
>你能把你的问题描述得再具体一点吗?你的cgi程序是在你自己的服务器上还是就
>是www.musi-cal.com/cgi-bin/query?你说的“重定向”是什么涵义?
>
>
>--
>Qiangning Hong
>
> __________________________
>( Ignore previous fortune. )
> --------------------------
> o
> o .
> .---. //
> Y|o o|Y//
> /_(i=i)K/
> ~()~*~()~
> (_)-(_)
>
> Darth
> Vader
> koala
>_______________________________________________
>python-chinese list
>python-chinese at lists.python.cn
>http://python.cn/mailman/listinfo/python-chinese
>
= = = = = = = = = = = = = = = = = = = =
致
礼!
陈锦
jeekchen at 163.com
2005-06-08
2005年06月08日 星期三 18:07
有些明白了,所有整数运算都产生新值,包括付值运算:
>>> a = 10
>>> b = a
>>> id(a), id(b)
(8017228, 8017228)
>>> a = 20
>>> id(a), id(b)
(8017108, 8017228)
>>>
所以用for虽然改变值了,但值在新的位置,list本身并不知道这个位置。
>>> a = range(10)
>>> a
[0, 1, 2, 3, 4, 5, 6, 7, 8, 9]
>>> for i in range(10):
print id(a[i])
8017348
8017336
8017324
8017312
8017300
8017288
8017276
8017264
8017252
8017240
>>> a[0] = 100
>>> for i in range(10):
print id(a[i])
10214556
8017336
8017324
8017312
8017300
8017288
8017276
8017264
8017252
8017240
>>>
所以只有用a[i],才能使list知道新值的位置。
cpunion wrote:
> 自己推翻这个模拟。add并不改变自己的值,实际这个我也很迷糊。
>
> cpunion wrote:
>
>> flyaflyaa wrote:
>>
>>>> 一、
>>>>
>>>>
>>>>>>> a = range(10)
>>>>>>> a
>>>>>>>
>>>>>>
>>>>>>
>>>> [0, 1, 2, 3, 4, 5, 6, 7, 8, 9]
>>>>
>>>>
>>>>>>> for i in a:
>>>>>>>
>>>>>>
>>>>>>
>>>> i += 1
>>>>
>>>>
>>>>>>> a
>>>>>>>
>>>>>>
>>>>>>
>>>> [0, 1, 2, 3, 4, 5, 6, 7, 8, 9]
>>>> 如果a中元素是数,用for就不能改变a中元素,只能用filter,map,reduce
>>>> 作 复杂
>>>> 处理了,如果a中元素是list,就可以。不统一。
>>>>
>>>
>>>
>> python把一些基本类型(包括string)都实现为const,模拟出const行为,即自
>> 身 值 不变,每个操作都会生成一个新对象。
>>
>> 它并没有什么不统一,恰恰相反,它非常统一,遍历时确实返回了元素的引
>> 用,整数值没有改变的原因是,整数的add操作没有改变它自身,而是返回另一
>> 个对 象, 可以写一个非常简单的模拟:
>>
>> >>> class Integer:
>> def __init__ (self, value):
>> self.value = value
>> def __add__ (self, value):
>> self.value += value
>> return self
>> def getValue (self):
>> return self.value
>>
>> >>> class ConstInteger:
>> def __init__ (self, value):
>> self.value = value
>> def __add__ (self, value):
>> return ConstInteger (self.value + value)
>> def getValue (self):
>> return self.value
>>
>> >>> a = [Integer(i) for i in range (10)]
>> >>> for i in a:
>> i += 1
>>
>> >>> for i in a:
>> print i.getValue ()
>>
>> 1
>> 2
>> 3
>> 4
>> 5
>> 6
>> 7
>> 8
>> 9
>> 10
>> >>> b = [ConstInteger(i) for i in range (10)]
>> >>> for i in b:
>> i += 1
>>
>> >>> for i in b:
>> print i.getValue ()
>>
>> 0
>> 1
>> 2
>> 3
>> 4
>> 5
>> 6
>> 7
>> 8
>> 9
>>
>> 可见并没有不统一的地方,它也能根据需求做出不同的实现,恰好证明这方面
>> 是 很 好用的。
>>
>>>
>>>
>> _______________________________________________
>> python-chinese list
>> python-chinese at lists.python.cn
>> http://python.cn/mailman/listinfo/python-chinese
>>
>>
> _______________________________________________
> python-chinese list
> python-chinese at lists.python.cn
> http://python.cn/mailman/listinfo/python-chinese
>
2005年06月09日 星期四 14:36
要是有 i++ 就好了..;) >>> i=1 >>> i+=2 >>> i 3 >>> i++ SyntaxError: invalid syntax >>> ++i 3 >>> ++i 3 >>> i+ SyntaxError: invalid syntax >>> +i 3 >>> i++i 6 >>> i++++i 6 >>> Neo Chan (netkiller) Amateur Radio Callsign: BG7NYT Best 73 de BG7NYT -----Original Message----- From: python-chinese-bounces at lists.python.cn [mailto:python-chinese-bounces at lists.python.cn] On Behalf Of flyaflyaa Sent: Wednesday, June 08, 2005 2:29 PM To: python-chinese at lists.python.cn Subject: [python-chinese] py的几处不爽 一、 >>> a = range(10) >>> a [0, 1, 2, 3, 4, 5, 6, 7, 8, 9] >>> for i in a: i += 1 >>> a [0, 1, 2, 3, 4, 5, 6, 7, 8, 9] 如果a中元素是数,用for就不能改变a中元素,只能用filter,map,reduce作复杂 处理了,如果a中元素是list,就可以。不统一。 二、 a = "abccdd" a[0] = 'c" 出错,想改a中的一个元素就要再生成一个新的string,这多慢 三、 函数默认参数是list的话,每次调用都用同一个list,会发生错误。 >>> def foo( a = []): a.append(10) print a >>> foo() [10] >>> foo() [10, 10] >>> 这个问题影响不大,可为什么不改掉,很容易产生错误 _______________________________________________ python-chinese list python-chinese at lists.python.cn http://python.cn/mailman/listinfo/python-chinese -------------- next part -------------- A non-text attachment was scrubbed... Name: Neo Chan.vcf Type: text/x-vcard Size: 1081 bytes Desc: not available Url : http://lists.exoweb.net/pipermail/python-chinese/attachments/20050609/b0792652/NeoChan.vcf
2005年06月09日 星期四 15:12
如果stirng不是immutable的, a = “abcd” b = a; a[0] = ‘c’, 你改动了a,但是b也鬼使神差地改动了,这是你期望的行为吗?如果是,那这也会 是别人期望的行为吗? 这也是为什么C++中向vector中push_back的时候会是copy语义。 看上去比较糟糕,实际上已经是一个不错的抉择了,十几年的Python,不会那么差 劲的。 -----Original Message----- From: python-chinese-bounces at lists.python.cn [mailto:python-chinese-bounces at lists.python.cn] On Behalf Of flyaflyaa Sent: Wednesday, June 08, 2005 2:29 PM To: python-chinese at lists.python.cn Subject: [python-chinese] py的几处不爽 一、 >>> a = range(10) >>> a [0, 1, 2, 3, 4, 5, 6, 7, 8, 9] >>> for i in a: i += 1 >>> a [0, 1, 2, 3, 4, 5, 6, 7, 8, 9] 如果a中元素是数,用for就不能改变a中元素,只能用filter,map,reduce作复杂 处理了,如果a中元素是list,就可以。不统一。 二、 a = "abccdd" a[0] = 'c" 出错,想改a中的一个元素就要再生成一个新的string,这多慢 三、 函数默认参数是list的话,每次调用都用同一个list,会发生错误。 >>> def foo( a = []): a.append(10) print a >>> foo() [10] >>> foo() [10, 10] >>> 这个问题影响不大,可为什么不改掉,很容易产生错误 _______________________________________________ python-chinese list python-chinese at lists.python.cn http://python.cn/mailman/listinfo/python-chinese
2005年06月09日 星期四 15:37
flyaflyaa wrote: > 一、 >>>> a = range(10) >>>> a > [0, 1, 2, 3, 4, 5, 6, 7, 8, 9] >>>> for i in a: > i += 1 >>>> a > [0, 1, 2, 3, 4, 5, 6, 7, 8, 9] > 如果a中元素是数,用for就不能改变a中元素,只能用filter,map,reduce作复杂 > 处理了,如果a中元素是list,就可以。不统一。 这是因为你没有理解+=操作符和名字绑定的概念。 当i是数字时:i += 1 --> i = i + 1, 这是对"i"这个名字进行重新绑定。 当i是list时:i += [1] --> i.extend([1]),调用的是i的方法,可以改变i的值。 你如果用i = i + [1]就都不会修改a的值了。 > 二、 > a = "abccdd" > a[0] = 'c" > 出错,想改a中的一个元素就要再生成一个新的string,这多慢 string都是immutable的,不然不能作为dict的key。 如果你不能一次创建string,请先使用list,把要改的东西都改好了,再使用join 生成string。 如果你一定想要mutable string,请仔细阅读:help(UserString.MutableString) google python mutable string,你会得到更多资料。 > 三、 > 函数默认参数是list的话,每次调用都用同一个list,会发生错误。 >>>> def foo( a = []): > a.append(10) > print a >>>> foo() > [10] >>>> foo() > [10, 10] >>>> > 这个问题影响不大,可为什么不改掉,很容易产生错误 默认参数是在定义函数时而不是在调用时创建的。这是feature不是bug。 -- Qiangning Hong __________________________________________________________ (how old is Ramareth? 12 I think ) ( <`Kumba> 67 i need a third response to form a ) ( mean ) ---------------------------------------------------------- o o .--. |o_o | |:_/ | // \ \ (| | ) /'\_ _/`\ \___)=(___/
2005年06月09日 星期四 16:12
flyaflyaa wrote:
>>一、
>>
>>
>>>>>a = range(10)
>>>>>a
>>>>>
>>>>>
>>[0, 1, 2, 3, 4, 5, 6, 7, 8, 9]
>>
>>
>>>>>for i in a:
>>>>>
>>>>>
>> i += 1
>>
>>
>>>>>a
>>>>>
>>>>>
>>[0, 1, 2, 3, 4, 5, 6, 7, 8, 9]
>>如果a中元素是数,用for就不能改变a中元素,只能用filter,map,reduce作复杂
>>处理了,如果a中元素是list,就可以。不统一。
>>
>>
python把一些基本类型(包括string)都实现为const,模拟出const行为,即自身值
不变,每个操作都会生成一个新对象。
它并没有什么不统一,恰恰相反,它非常统一,遍历时确实返回了元素的引用,整
数值没有改变的原因是,整数的add操作没有改变它自身,而是返回另一个对象,
可以写一个非常简单的模拟:
>>> class Integer:
def __init__ (self, value):
self.value = value
def __add__ (self, value):
self.value += value
return self
def getValue (self):
return self.value
>>> class ConstInteger:
def __init__ (self, value):
self.value = value
def __add__ (self, value):
return ConstInteger (self.value + value)
def getValue (self):
return self.value
>>> a = [Integer(i) for i in range (10)]
>>> for i in a:
i += 1
>>> for i in a:
print i.getValue ()
1
2
3
4
5
6
7
8
9
10
>>> b = [ConstInteger(i) for i in range (10)]
>>> for i in b:
i += 1
>>> for i in b:
print i.getValue ()
0
1
2
3
4
5
6
7
8
9
可见并没有不统一的地方,它也能根据需求做出不同的实现,恰好证明这方面是很
好用的。
>
>
2005年06月09日 星期四 16:38
自己推翻这个模拟。add并不改变自己的值,实际这个我也很迷糊。 cpunion wrote: > flyaflyaa wrote: > >>> 一、 >>> >>> >>>>>> a = range(10) >>>>>> a >>>>>> >>>>> >>> [0, 1, 2, 3, 4, 5, 6, 7, 8, 9] >>> >>> >>>>>> for i in a: >>>>>> >>>>> >>> i += 1 >>> >>> >>>>>> a >>>>>> >>>>> >>> [0, 1, 2, 3, 4, 5, 6, 7, 8, 9] >>> 如果a中元素是数,用for就不能改变a中元素,只能用filter,map,reduce作 >>> 复杂 >>> 处理了,如果a中元素是list,就可以。不统一。 >>> >> > python把一些基本类型(包括string)都实现为const,模拟出const行为,即自身 > 值 不变,每个操作都会生成一个新对象。 > > 它并没有什么不统一,恰恰相反,它非常统一,遍历时确实返回了元素的引用, > 整数值没有改变的原因是,整数的add操作没有改变它自身,而是返回另一个对 > 象, 可以写一个非常简单的模拟: > > >>> class Integer: > def __init__ (self, value): > self.value = value > def __add__ (self, value): > self.value += value > return self > def getValue (self): > return self.value > > >>> class ConstInteger: > def __init__ (self, value): > self.value = value > def __add__ (self, value): > return ConstInteger (self.value + value) > def getValue (self): > return self.value > > >>> a = [Integer(i) for i in range (10)] > >>> for i in a: > i += 1 > > >>> for i in a: > print i.getValue () > > 1 > 2 > 3 > 4 > 5 > 6 > 7 > 8 > 9 > 10 > >>> b = [ConstInteger(i) for i in range (10)] > >>> for i in b: > i += 1 > > >>> for i in b: > print i.getValue () > > 0 > 1 > 2 > 3 > 4 > 5 > 6 > 7 > 8 > 9 > > 可见并没有不统一的地方,它也能根据需求做出不同的实现,恰好证明这方面是 > 很 好用的。 > >> >> > _______________________________________________ > python-chinese list > python-chinese at lists.python.cn > http://python.cn/mailman/listinfo/python-chinese > >
2005年06月09日 星期四 18:22
i只是个引用名字,当调用i=...时,它会指向不同的对象,所以i+=1时,它所引用 的对象其实是变化了的。但i是list时,为何处理方式不一样呢(我已经推翻前面 的看法,和帖主看法一样了)? 我查看了一点python源码,+=运算符在内部其实是做了不同处理,对于数字类型 (或其它普遍类型),大概会转换成i = i + ..;对于序列类型,它会并不转换成 i = i + ..而是调用concat直接处理操作符左侧的对象。 +=操作符好像没办法重写? flyaflyaa wrote: > 有些明白了,所有整数运算都产生新值,包括付值运算: > >>> a = 10 > >>> b = a > >>> id(a), id(b) > (8017228, 8017228) > >>> a = 20 > >>> id(a), id(b) > (8017108, 8017228) > >>> > > 所以用for虽然改变值了,但值在新的位置,list本身并不知道这个位置。 > >>> a = range(10) > >>> a > [0, 1, 2, 3, 4, 5, 6, 7, 8, 9] > >>> for i in range(10): > print id(a[i]) > > 8017348 > 8017336 > 8017324 > 8017312 > 8017300 > 8017288 > 8017276 > 8017264 > 8017252 > 8017240 > >>> a[0] = 100 > >>> for i in range(10): > print id(a[i]) > > 10214556 > 8017336 > 8017324 > 8017312 > 8017300 > 8017288 > 8017276 > 8017264 > 8017252 > 8017240 > >>> > 所以只有用a[i],才能使list知道新值的位置。 > > cpunion wrote: > >> 自己推翻这个模拟。add并不改变自己的值,实际这个我也很迷糊。 >> >> cpunion wrote: >> >>> flyaflyaa wrote: >>> >>>>> 一、 >>>>> >>>>> >>>>>>>> a = range(10) >>>>>>>> a >>>>>>>> >>>>>>> >>>>>>> >>>>>>> >>>>> [0, 1, 2, 3, 4, 5, 6, 7, 8, 9] >>>>> >>>>> >>>>>>>> for i in a: >>>>>>>> >>>>>>> >>>>>>> >>>>>>> >>>>> i += 1 >>>>> >>>>> >>>>>>>> a >>>>>>>> >>>>>>> >>>>>>> >>>>>>> >>>>> [0, 1, 2, 3, 4, 5, 6, 7, 8, 9] >>>>> 如果a中元素是数,用for就不能改变a中元素,只能用filter,map,reduce >>>>> 作 复杂 >>>>> 处理了,如果a中元素是list,就可以。不统一。 >>>>> >>>> >>>> >>>> >>> python把一些基本类型(包括string)都实现为const,模拟出const行为,即自 >>> 身 值 不变,每个操作都会生成一个新对象。 >>> >>> 它并没有什么不统一,恰恰相反,它非常统一,遍历时确实返回了元素的引 >>> 用,整数值没有改变的原因是,整数的add操作没有改变它自身,而是返回另 >>> 一个对 象, 可以写一个非常简单的模拟: >>> >>> >>> class Integer: >>> def __init__ (self, value): >>> self.value = value >>> def __add__ (self, value): >>> self.value += value >>> return self >>> def getValue (self): >>> return self.value >>> >>> >>> class ConstInteger: >>> def __init__ (self, value): >>> self.value = value >>> def __add__ (self, value): >>> return ConstInteger (self.value + value) >>> def getValue (self): >>> return self.value >>> >>> >>> a = [Integer(i) for i in range (10)] >>> >>> for i in a: >>> i += 1 >>> >>> >>> for i in a: >>> print i.getValue () >>> >>> 1 >>> 2 >>> 3 >>> 4 >>> 5 >>> 6 >>> 7 >>> 8 >>> 9 >>> 10 >>> >>> b = [ConstInteger(i) for i in range (10)] >>> >>> for i in b: >>> i += 1 >>> >>> >>> for i in b: >>> print i.getValue () >>> >>> 0 >>> 1 >>> 2 >>> 3 >>> 4 >>> 5 >>> 6 >>> 7 >>> 8 >>> 9 >>> >>> 可见并没有不统一的地方,它也能根据需求做出不同的实现,恰好证明这方面 >>> 是 很 好用的。 >>> >>>> >>>> >>> _______________________________________________ >>> python-chinese list >>> python-chinese at lists.python.cn >>> http://python.cn/mailman/listinfo/python-chinese >>> >>> >> _______________________________________________ >> python-chinese list >> python-chinese at lists.python.cn >> http://python.cn/mailman/listinfo/python-chinese >> > > _______________________________________________ > python-chinese list > python-chinese at lists.python.cn > http://python.cn/mailman/listinfo/python-chinese > >
2005年06月09日 星期四 19:55
+= 相当于执行了 __iadd__( self, other) 看 Python Reference Manual 中的3.3.7 Emulating numeric types 2005/6/9, cpunion <cpunion at 263.net>: > i只是个引用名字,当调用i=...时,它会指向不同的对象,所以i+=1时,它所引用 > 的对象其实是变化了的。但i是list时,为何处理方式不一样呢(我已经推翻前面 > 的看法,和帖主看法一样了)? > > 我查看了一点python源码,+=运算符在内部其实是做了不同处理,对于数字类型 > (或其它普遍类型),大概会转换成i = i + ..;对于序列类型,它会并不转换成 > i = i + ..而是调用concat直接处理操作符左侧的对象。 > > +=操作符好像没办法重写? > > flyaflyaa wrote: > > > 有些明白了,所有整数运算都产生新值,包括付值运算: > > >>> a = 10 > > >>> b = a > > >>> id(a), id(b) > > (8017228, 8017228) > > >>> a = 20 > > >>> id(a), id(b) > > (8017108, 8017228) > > >>> > > > > 所以用for虽然改变值了,但值在新的位置,list本身并不知道这个位置。 > > >>> a = range(10) > > >>> a > > [0, 1, 2, 3, 4, 5, 6, 7, 8, 9] > > >>> for i in range(10): > > print id(a[i]) > > > > 8017348 > > 8017336 > > 8017324 > > 8017312 > > 8017300 > > 8017288 > > 8017276 > > 8017264 > > 8017252 > > 8017240 > > >>> a[0] = 100 > > >>> for i in range(10): > > print id(a[i]) > > > > 10214556 > > 8017336 > > 8017324 > > 8017312 > > 8017300 > > 8017288 > > 8017276 > > 8017264 > > 8017252 > > 8017240 > > >>> > > 所以只有用a[i],才能使list知道新值的位置。 > > > > cpunion wrote: > > > >> 自己推翻这个模拟。add并不改变自己的值,实际这个我也很迷糊。 > >> > >> cpunion wrote: > >> > >>> flyaflyaa wrote: > >>> > >>>>> 一、 > >>>>> > >>>>> > >>>>>>>> a = range(10) > >>>>>>>> a > >>>>>>>> > >>>>>>> > >>>>>>> > >>>>>>> > >>>>> [0, 1, 2, 3, 4, 5, 6, 7, 8, 9] > >>>>> > >>>>> > >>>>>>>> for i in a: > >>>>>>>> > >>>>>>> > >>>>>>> > >>>>>>> > >>>>> i += 1 > >>>>> > >>>>> > >>>>>>>> a > >>>>>>>> > >>>>>>> > >>>>>>> > >>>>>>> > >>>>> [0, 1, 2, 3, 4, 5, 6, 7, 8, 9] > >>>>> 如果a中元素是数,用for就不能改变a中元素,只能用filter,map,reduce > >>>>> 作 复杂 > >>>>> 处理了,如果a中元素是list,就可以。不统一。 > >>>>> > >>>> > >>>> > >>>> > >>> python把一些基本类型(包括string)都实现为const,模拟出const行为,即自 > >>> 身 值 不变,每个操作都会生成一个新对象。 > >>> > >>> 它并没有什么不统一,恰恰相反,它非常统一,遍历时确实返回了元素的引 > >>> 用,整数值没有改变的原因是,整数的add操作没有改变它自身,而是返回另 > >>> 一个对 象, 可以写一个非常简单的模拟: > >>> > >>> >>> class Integer: > >>> def __init__ (self, value): > >>> self.value = value > >>> def __add__ (self, value): > >>> self.value += value > >>> return self > >>> def getValue (self): > >>> return self.value > >>> > >>> >>> class ConstInteger: > >>> def __init__ (self, value): > >>> self.value = value > >>> def __add__ (self, value): > >>> return ConstInteger (self.value + value) > >>> def getValue (self): > >>> return self.value > >>> > >>> >>> a = [Integer(i) for i in range (10)] > >>> >>> for i in a: > >>> i += 1 > >>> > >>> >>> for i in a: > >>> print i.getValue () > >>> > >>> 1 > >>> 2 > >>> 3 > >>> 4 > >>> 5 > >>> 6 > >>> 7 > >>> 8 > >>> 9 > >>> 10 > >>> >>> b = [ConstInteger(i) for i in range (10)] > >>> >>> for i in b: > >>> i += 1 > >>> > >>> >>> for i in b: > >>> print i.getValue () > >>> > >>> 0 > >>> 1 > >>> 2 > >>> 3 > >>> 4 > >>> 5 > >>> 6 > >>> 7 > >>> 8 > >>> 9 > >>> > >>> 可见并没有不统一的地方,它也能根据需求做出不同的实现,恰好证明这方面 > >>> 是 很 好用的。 > >>> > >>>> > >>>> > >>> _______________________________________________ > >>> python-chinese list > >>> python-chinese at lists.python.cn > >>> http://python.cn/mailman/listinfo/python-chinese > >>> > >>> > >> _______________________________________________ > >> python-chinese list > >> python-chinese at lists.python.cn > >> http://python.cn/mailman/listinfo/python-chinese > >> > > > > _______________________________________________ > > python-chinese list > > python-chinese at lists.python.cn > > http://python.cn/mailman/listinfo/python-chinese > > > > > _______________________________________________ > python-chinese list > python-chinese at lists.python.cn > http://python.cn/mailman/listinfo/python-chinese > -- I like python! My Donews Blog: http://www.donews.net/limodou New Google Maillist: http://groups-beta.google.com/group/python-cn
2005年06月09日 星期四 20:05
cpunion wrote:
> i只是个引用名字,当调用i=...时,它会指向不同的对象,所以i+=1时,它所引用
> 的对象其实是变化了的。但i是list时,为何处理方式不一样呢(我已经推翻前面
> 的看法,和帖主看法一样了)?
>
> 我查看了一点python源码,+=运算符在内部其实是做了不同处理,对于数字类型
> (或其它普遍类型),大概会转换成i = i + ..;对于序列类型,它会并不转换成
> i = i + ..而是调用concat直接处理操作符左侧的对象。
>
> +=操作符好像没办法重写?
x+=y 会被解析成 x.__iadd__(y),如果x没有__iadd__方法则解析成x = x.__add__(y)
list有__iadd__方法,int没有,这就是差别。
重载+=操作符可以通过重载__iadd__方法实现。详细请看Python Reference
Manual中3.3.7节:Emulating numeric types。
--
Qiangning Hong
_______________________________________________________
/ Beauty is truth, truth beauty, that is all Ye know on \
| earth, and all ye need to know. |
| |
\ -- John Keats /
-------------------------------------------------------
\
\
____
/# /_\_
| |/o\o\
| \\_/_/
/ |_ |
| ||\_ ~|
| ||| \/
| |||_
\// |
|| |
||_ \
\_| o|
/\___/
/ ||||__
(___)_)
2005年06月10日 星期五 09:24
Great Discuss! 习惯思路在Python 中也许并不相合与设计! 大家可以回想自个儿原先的编程思路在Python 中的实际行为…………是种非常好的提高途径哪! http://wiki.woodpecker.org.cn/moin/NoNicelyPy 汇集起来大家的讨论了! 2005/6/9, Qiangning Hong <hongqn at gmail.com>: > cpunion wrote: > > i只是个引用名字,当调用i=...时,它会指向不同的对象,所以i+=1时,它所引用 > > 的对象其实是变化了的。但i是list时,为何处理方式不一样呢(我已经推翻前面 > > 的看法,和帖主看法一样了)? > > > > > 我查看了一点python源码,+=运算符在内部其实是做了不同处理,对于数字类型 > > (或其它普遍类型),大概会转换成i = i + ..;对于序列类型,它会并不转换成 > > i = i + ..而是调用concat直接处理操作符左侧的对象。 > > > > +=操作符好像没办法重写? > > x+=y 会被解析成 x.__iadd__(y),如果x没有__iadd__方法则解析成x = x.__add__(y) > > list有__iadd__方法,int没有,这就是差别。 > > 重载+=操作符可以通过重载__iadd__方法实现。详细请看Python Reference > Manual中3.3.7节:Emulating numeric types。 > > -- > Qiangning Hong > > _______________________________________________________ > / Beauty is truth, truth beauty, that is all Ye know on \ > | earth, and all ye need to know. | > | | > \ -- John Keats / > ------------------------------------------------------- > \ > \ > ____ > /# /_\_ > | |/o\o\ > | \\_/_/ > / |_ | > | ||\_ ~| > | ||| \/ > | |||_ > \// | > || | > ||_ \ > \_| o| > /\___/ > / ||||__ > (___)_) > _______________________________________________ > python-chinese list > python-chinese at lists.python.cn > http://python.cn/mailman/listinfo/python-chinese > -- [Time is unimportant, only life important!]
Zeuux © 2025
京ICP备05028076号