help, thanks very much.

classic Classic list List threaded Threaded
2 messages Options
Reply | Threaded
Open this post in threaded view
|

help, thanks very much.

bill.wu
Mail
我想问一下,这两个函数的的 def func(x)和func() 一个带X一个不带X,请问是什么区别?为什么?
 
 
(1)
 
def func(x):
    print 'x is', x
    x = 2
    print 'Changed local x to', x

x = 50
func(x)
print 'x is still', x
(2)
 
def func():
    global x

    print 'x is', x
    x = 2
    print 'Changed local x to', x

x = 50
func()
print 'Value of x is', x
____________________________________________________________________
____________________________________________
DreamMail - 电子邮件新体验 www.dreammail.org

_______________________________________________
python-chinese
Post: send [hidden email]
Subscribe: send subscribe to [hidden email]
Unsubscribe: send unsubscribe to  [hidden email]
Detail Info: http://python.cn/mailman/listinfo/python-chinese
Reply | Threaded
Open this post in threaded view
|

Re: help, thanks very much.

ww-3
func(x)的x是参数,是调用函数是传给它的,不带x的函数,如果直接在函数内部
使用x,是局部变量,所以要用global特地说明一下,是引用函数外面定义的变量
,这两个函数的结果是一样的。

On Wed, 16 Jan 2008 09:54:42 +0800
"bill.wu" <[hidden email]> wrote:

> 我想问一下,这两个函数的的 def func(x)和func() 一个带X一个不带X,请问是什么区别?为什么?
>
>
> (1)
>
> def func(x):
>     print 'x is', x
>     x = 2
>     print 'Changed local x to', x
>
> x = 50
> func(x)
> print 'x is still', x (2)
>
> def func():
>     global x
>
>     print 'x is', x
>     x = 2
>     print 'Changed local x to', x
>
> x = 50
> func()
> print 'Value of x is', x
>
> ____________________________________________________________________
>
> ____________________________________________
> DreamMail - 电子邮件新体验 www.dreammail.org

--
ww <[hidden email]>

_______________________________________________
python-chinese
Post: send [hidden email]
Subscribe: send subscribe to [hidden email]
Unsubscribe: send unsubscribe to  [hidden email]
Detail Info: http://python.cn/mailman/listinfo/python-chinese