2007年03月30日 星期五 11:00
各位说说下面的程序应该输出什么?
class A:
def __init__(self,m={}):
self.x=m
while True:
a=A()
print a.x
a.x['1']=2
--
devdoer
devdoer at gmail.com
http://devdoer.blog.sohu.com/
-------------- next part --------------
An HTML attachment was scrubbed...
URL: http://python.cn/pipermail/python-chinese/attachments/20070330/11f805d9/attachment.html
2007年03月30日 星期五 11:08
第一行是空字典,后面就不是空的了 就是告诉你函数参数的缺省值,不是每次都重新生成新对象,而是一直用一个 在 07-3-30,bird devdoer<devdoer at gmail.com> 写道: > 各位说说下面的程序应该输出什么? > class A: > def __init__(self,m={}): > self.x=m > > while True: > a=A() > print a.x > a.x['1']=2 > > -- > devdoer > devdoer at gmail.com > http://devdoer.blog.sohu.com/ > _______________________________________________ > python-chinese > Post: send python-chinese at lists.python.cn > Subscribe: send subscribe to > python-chinese-request at lists.python.cn > Unsubscribe: send unsubscribe to > python-chinese-request at lists.python.cn > Detail Info: > http://python.cn/mailman/listinfo/python-chinese >
2007年03月30日 星期五 11:19
函数的参数问题
在 Python Tutorial 有这样一段话
Important warning: The default value is evaluated only once. This
makes a difference when the default is a mutable object such as a
list, dictionary, or instances of most classes. For example, the
following function accumulates the arguments passed to it on
subsequent calls:
def f(a, L=[]):
L.append(a)
return L
print f(1)
print f(2)
print f(3)
This will print
[1]
[1, 2]
[1, 2, 3]
If you don't want the default to be shared between subsequent calls,
you can write the function like this instead:
def f(a, L=None):
if L is None:
L = []
L.append(a)
return L
On 3/30/07, bird devdoer <devdoer at gmail.com> wrote:
> 各位说说下面的程序应该输出什么?
> class A:
> def __init__(self,m={}):
> self.x=m
>
> while True:
> a=A()
> print a.x
> a.x['1']=2
>
2007年03月30日 星期五 11:20
很特别,跟直观的理解很不一致。 一般的理解都是创建a对象时,缺省参数是个空字典对象。但是现在看来,缺省的字典对象只是构造一次。 在07-3-30,helium <helium.sun at gmail.com> 写道: > > 第一行是空字典,后面就不是空的了 > > 就是告诉你函数参数的缺省值,不是每次都重新生成新对象,而是一直用一个 > > 在 07-3-30,bird devdoer<devdoer at gmail.com> 写道: > > 各位说说下面的程序应该输出什么? > > class A: > > def __init__(self,m={}): > > self.x=m > > > > while True: > > a=A() > > print a.x > > a.x['1']=2 > > > > -- > > devdoer > > devdoer at gmail.com > > http://devdoer.blog.sohu.com/ > > _______________________________________________ > > python-chinese > > Post: send python-chinese at lists.python.cn > > Subscribe: send subscribe to > > python-chinese-request at lists.python.cn > > Unsubscribe: send unsubscribe to > > python-chinese-request at lists.python.cn > > Detail Info: > > http://python.cn/mailman/listinfo/python-chinese > > > _______________________________________________ > python-chinese > Post: send python-chinese at lists.python.cn > Subscribe: send subscribe to python-chinese-request at lists.python.cn > Unsubscribe: send unsubscribe to python-chinese-request at lists.python.cn > Detail Info: http://python.cn/mailman/listinfo/python-chinese -- devdoer devdoer at gmail.com http://devdoer.blog.sohu.com/ -------------- next part -------------- An HTML attachment was scrubbed... URL: http://python.cn/pipermail/python-chinese/attachments/20070330/d94c68ba/attachment.html
2007年03月30日 星期五 11:24
多谢给出资料,以后用缺省参数时需要特别注意了。 2007/3/30, lubiao <lubiao.py at gmail.com>: > > 函数的参数问题 > 在 Python Tutorial 有这样一段话 > > > Important warning: The default value is evaluated only once. This > makes a difference when the default is a mutable object such as a > list, dictionary, or instances of most classes. For example, the > following function accumulates the arguments passed to it on > subsequent calls: > > > def f(a, L=[]): > L.append(a) > return L > > print f(1) > print f(2) > print f(3) > > This will print > > > [1] > [1, 2] > [1, 2, 3] > > If you don't want the default to be shared between subsequent calls, > you can write the function like this instead: > > > def f(a, L=None): > if L is None: > L = [] > L.append(a) > return L > > > > On 3/30/07, bird devdoer <devdoer at gmail.com> wrote: > > 各位说说下面的程序应该输出什么? > > class A: > > def __init__(self,m={}): > > self.x=m > > > > while True: > > a=A() > > print a.x > > a.x['1']=2 > > > _______________________________________________ > python-chinese > Post: send python-chinese at lists.python.cn > Subscribe: send subscribe to python-chinese-request at lists.python.cn > Unsubscribe: send unsubscribe to python-chinese-request at lists.python.cn > Detail Info: http://python.cn/mailman/listinfo/python-chinese -- devdoer devdoer at gmail.com http://devdoer.blog.sohu.com/ -------------- next part -------------- An HTML attachment was scrubbed... URL: http://python.cn/pipermail/python-chinese/attachments/20070330/414b51cf/attachment.html
2007年03月30日 星期五 15:33
¼ÇÏÂÁË£¬Ð»Ð» ²»¹ýÕâµãȷʵºÜÈÝÒ×Ôì³ÉÎó½â°¡ ÔÚ07-3-30£¬bird devdoer <devdoer在gmail.com> дµÀ£º > > ¸÷λ˵˵ÏÂÃæµÄ³ÌÐòÓ¦¸ÃÊä³öʲô£¿ > class A: > def __init__(self,m={}): > self.x=m > > while True: > a=A() > print a.x > a.x['1']=2 > > -- > devdoer > devdoer在gmail.com > http://devdoer.blog.sohu.com/ > _______________________________________________ > python-chinese > Post: send python-chinese在lists.python.cn > Subscribe: send subscribe to python-chinese-request在lists.python.cn > Unsubscribe: send unsubscribe to python-chinese-request在lists.python.cn > Detail Info: http://python.cn/mailman/listinfo/python-chinese > -------------- 下一部分 -------------- Ò»¸öHTML¸½¼þ±»ÒƳý... URL: http://python.cn/pipermail/python-chinese/attachments/20070330/3cda7f86/attachment.htm
Zeuux © 2025
京ICP备05028076号