Python论坛  - 讨论区

标题:[python-chinese] 我不能理解的python程序

2007年03月30日 星期五 11:00

bird devdoer devdoer在gmail.com
星期五 三月 30 11:00:47 HKT 2007

各位说说下面的程序应该输出什么?
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 

[导入自Mailman归档:http://www.zeuux.org/pipermail/zeuux-python]

2007年03月30日 星期五 11:08

helium helium.sun在gmail.com
星期五 三月 30 11:08:43 HKT 2007

第一行是空字典,后面就不是空的了

就是告诉你函数参数的缺省值,不是每次都重新生成新对象,而是一直用一个

在 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
>

[导入自Mailman归档:http://www.zeuux.org/pipermail/zeuux-python]

2007年03月30日 星期五 11:19

lubiao lubiao.py在gmail.com
星期五 三月 30 11:19:01 HKT 2007

函数的参数问题
在 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
>

[导入自Mailman归档:http://www.zeuux.org/pipermail/zeuux-python]

2007年03月30日 星期五 11:20

bird devdoer devdoer在gmail.com
星期五 三月 30 11:20:25 HKT 2007

很特别,跟直观的理解很不一致。
一般的理解都是创建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 

[导入自Mailman归档:http://www.zeuux.org/pipermail/zeuux-python]

2007年03月30日 星期五 11:24

bird devdoer devdoer在gmail.com
星期五 三月 30 11:24:20 HKT 2007

多谢给出资料,以后用缺省参数时需要特别注意了。

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 

[导入自Mailman归档:http://www.zeuux.org/pipermail/zeuux-python]

2007年03月30日 星期五 15:33

Deng Patrick kedeng19801002在gmail.com
星期五 三月 30 15:33:22 HKT 2007

¼ÇÏÂÁË£¬Ð»Ð»
²»¹ýÕâµãȷʵºÜÈÝÒ×Ôì³ÉÎó½â°¡


ÔÚ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 

[导入自Mailman归档:http://www.zeuux.org/pipermail/zeuux-python]

如下红色区域有误,请重新填写。

    你的回复:

    请 登录 后回复。还没有在Zeuux哲思注册吗?现在 注册 !

    Zeuux © 2025

    京ICP备05028076号