2005年08月23日 星期二 18:37
*hi all,* *我这里有个代码,* *#!/usr/bin/python* *def func(x):* *print 'x is', x* * x = 2* * print 'Changed local x to', x* ** *x = 50* *func(x)* *print 'x is still', x* ** *为什么输出会是* x is 50 Changed local x to 2 x is still 50 为什么第一个x is 50用的会是function外的x = 50呢? 而非x = 2? 请教!多谢! -------------- next part -------------- An HTML attachment was scrubbed... URL: http://lists.exoweb.net/pipermail/python-chinese/attachments/20050823/9343d1d5/attachment.html
2005年08月23日 星期二 18:57
变量的作用域问题…… 函数里外的x不一样,当然出现这种情况了。 On 8/23/05, Deslay <deslay at gmail.com> wrote: > > *hi all,* > *我这里有个代码,* > *#!/usr/bin/python* > *def func(x):* > *print 'x is', x* > * x = 2* > * print 'Changed local x to', x* > ** > *x = 50* > *func(x)* > *print 'x is still', x* > ** > *为什么输出会是* > x is 50 > Changed local x to 2 > x is still 50 > 为什么第一个x is 50用的会是function外的x = 50呢? > 而非x = 2? > 请教!多谢! > > _______________________________________________ > python-chinese list > python-chinese at lists.python.cn > http://python.cn/mailman/listinfo/python-chinese > > > -- ############################################# # "your future depends on your dreams." so go to sleep. # save water. shower with your girlfriend. # # Welcom2 : http//blog.csdn.net/auding1900/ ############################################# -------------- next part -------------- An HTML attachment was scrubbed... URL: http://lists.exoweb.net/pipermail/python-chinese/attachments/20050823/1751d9f8/attachment-0001.htm
2005年08月23日 星期二 19:02
1NogMDUtOC0yM6OsRGVzbGF5PGRlc2xheUBnbWFpbC5jb20+INC0tcCjugo+IGhpIGFsbCwgCj4g ztLV4sDv09C49rT6wussIAo+ICMhL3Vzci9iaW4vcHl0aG9uIAo+IGRlZiBmdW5jKHgpOiAKPiAg IHByaW50ICd4IGlzJywgeCAKPiAgIHggPSAyIAo+ICAgcHJpbnQgICdDaGFuZ2VkIGxvY2FsIHgg dG8nLCB4IAo+ICAgCj4geCA9IDUwIAo+IGZ1bmMoeCkgCj4gcHJpbnQgJ3ggaXMgc3RpbGwnLCB4 IAo+ICAgCj4gzq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005年08月23日 星期二 20:14
在 05-8-23,Deslay<deslay at gmail.com> 写道: > hi all, > 我这里有个代码, > #!/usr/bin/python > def func(x): > print 'x is', x > x = 2 > print 'Changed local x to', x > > x = 50 > func(x) > print 'x is still', x > > 为什么输出会是 > x is 50 > Changed local x to 2 > x is still 50 > 为什么第一个x is 50用的会是function外的x = 50呢? > 而非x = 2? > > 请教!多谢! > Python中的参数是值参,而不是变参。因此在函数中的对参数的修改,并不影响外面的原值。 -- I like python! My Donews Blog: http://www.donews.net/limodou
2005年08月23日 星期二 21:32
多谢各位. missv <lj0508 at gmail.com> to python-chinese More options 7:38 pm (1½ hours ago) > def func(x): > print 'x is', x #这个x是函数参数x,值为50 > x = 2 #这个x是函数内的x,作用域从这一句开始,并覆盖参数x > print 'Changed local x to', x 这个我想我看明白了的. On 8/23/05, limodou <limodou at gmail.com> wrote: > > 在 05-8-23,Deslay<deslay at gmail.com> 写道: > > hi all, > > 我这里有个代码, > > #!/usr/bin/python > > def func(x): > > print 'x is', x > > x = 2 > > print 'Changed local x to', x > > > > x = 50 > > func(x) > > print 'x is still', x > > > > 为什么输出会是 > > x is 50 > > Changed local x to 2 > > x is still 50 > > 为什么第一个x is 50用的会是function外的x = 50呢? > > 而非x = 2? > > > > 请教!多谢! > > > > Python中的参数是值参,而不是变参。因此在函数中的对参数的修改,并不影响外面的原值。 > > -- > I like python! > My Donews Blog: http://www.donews.net/limodou > > _______________________________________________ > python-chinese list > python-chinese at lists.python.cn > http://python.cn/mailman/listinfo/python-chinese > > > -- ---------------------------------------------- Name: Hugh Rex Blog: http://www.hughr.org/ Location: Huizhou CHINA Matrix: MESS WITH THE BESS DIE LIKE THE REST ---------------------------------------------- -------------- next part -------------- An HTML attachment was scrubbed... URL: http://lists.exoweb.net/pipermail/python-chinese/attachments/20050823/17260f84/attachment.html
2005年08月24日 星期三 09:17
使用global语句,告诉Python这个变量名不是局部的,而是全局的。 def func(x): global x print 'x is', x x = 2 print 'Changed local x to', x x=50 func() print 'x is still', x x is 50 Changed local x to 2 x is still 2 2005/8/23, Deslay <deslay at gmail.com>: > 多谢各位. > missv > <lj0508 at gmail.com> to python-chinese More options 7:38 pm (1½ hours ago) > > def func(x): > > print 'x is', x #这个x是函数参数x,值为50 > > x = 2 #这个x是函数内的x,作用域从这一句开始,并覆盖参数x > > print 'Changed local x to', x这个我想我看明白了的. > > > On 8/23/05, limodou <limodou at gmail.com> wrote: > > > > 在 05-8-23,Deslay<deslay at gmail.com> 写道: > > > hi all, > > > 我这里有个代码, > > > #!/usr/bin/python > > > def func(x): > > > print 'x is', x > > > x = 2 > > > print 'Changed local x to', x > > > > > > x = 50 > > > func(x) > > > print 'x is still', x > > > > > > 为什么输出会是 > > > x is 50 > > > Changed local x to 2 > > > x is still 50 > > > 为什么第一个x is 50用的会是function外的x = 50呢? > > > 而非x = 2? > > > > > > 请教!多谢! > > > > > > > Python中的参数是值参,而不是变参。因此在函数中的对参数的修改,并不影响外面的原值。 > > > > -- > > I like python! > > My Donews Blog: http://www.donews.net/limodou > > > > _______________________________________________ > > python-chinese list > > python-chinese at lists.python.cn > > http://python.cn/mailman/listinfo/python-chinese > > > > > > > > > > -- > ---------------------------------------------- > Name: Hugh Rex > Blog: http://www.hughr.org/ > Location: Huizhou CHINA > Matrix: MESS WITH THE BESS DIE LIKE THE REST > ---------------------------------------------- > _______________________________________________ > python-chinese list > python-chinese at lists.python.cn > http://python.cn/mailman/listinfo/python-chinese > > >
2005年08月24日 星期三 09:28
阁下的Python什么版本?我的2.4,编译通不过,global不能这么用 On 8/24/05, D Shi Ming <dushmo at gmail.com> wrote: > > 使用global语句,告诉Python这个变量名不是局部的,而是全局的。 > def func(x): > global x > print 'x is', x > x = 2 > print 'Changed local x to', x > > x=50 > func() > print 'x is still', x > > x is 50 > Changed local x to 2 > x is still 2 > > > 2005/8/23, Deslay <deslay at gmail.com>: > > 多谢各位. > > missv > > <lj0508 at gmail.com> to python-chinese More options 7:38 pm (1½ hours ago) > > > def func(x): > > > print 'x is', x #这个x是函数参数x,值为50 > > > x = 2 #这个x是函数内的x,作用域从这一句开始,并覆盖参数x > > > print 'Changed local x to', x这个我想我看明白了的. > > > > > > On 8/23/05, limodou <limodou at gmail.com> wrote: > > > > > > 在 05-8-23,Deslay<deslay at gmail.com> 写道: > > > > hi all, > > > > 我这里有个代码, > > > > #!/usr/bin/python > > > > def func(x): > > > > print 'x is', x > > > > x = 2 > > > > print 'Changed local x to', x > > > > > > > > x = 50 > > > > func(x) > > > > print 'x is still', x > > > > > > > > 为什么输出会是 > > > > x is 50 > > > > Changed local x to 2 > > > > x is still 50 > > > > 为什么第一个x is 50用的会是function外的x = 50呢? > > > > 而非x = 2? > > > > > > > > 请教!多谢! > > > > > > > > > > Python中的参数是值参,而不是变参。因此在函数中的对参数的修改,并不影响外面的原值。 > > > > > > -- > > > I like python! > > > My Donews Blog: http://www.donews.net/limodou > > > > > > _______________________________________________ > > > python-chinese list > > > python-chinese at lists.python.cn > > > http://python.cn/mailman/listinfo/python-chinese > > > > > > > > > > > > > > > > > -- > > ---------------------------------------------- > > Name: Hugh Rex > > Blog: http://www.hughr.org/ > > Location: Huizhou CHINA > > Matrix: MESS WITH THE BESS DIE LIKE THE REST > > ---------------------------------------------- > > _______________________________________________ > > 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 > > > -- ############################################# # "your future depends on your dreams." so go to sleep. # save water. shower with your girlfriend. # # Welcom2 : http//blog.csdn.net/auding1900/ ############################################# -------------- next part -------------- An HTML attachment was scrubbed... URL: http://lists.exoweb.net/pipermail/python-chinese/attachments/20050824/b36bd7cd/attachment.html
2005年08月24日 星期三 09:37
我使用的是Python 2.4.1 2005/8/24, Spirit Auding <auding at gmail.com>: > 阁下的Python什么版本?我的2.4,编译通不过,global不能这么用 > > > On 8/24/05, D Shi Ming <dushmo at gmail.com > wrote: > > 使用global语句,告诉Python这个变量名不是局部的,而是全局的。 > > def func(x): > > global x > > print 'x is', x > > x = 2 > > print 'Changed local x to', x > > > > x=50 > > func() > > print 'x is still', x > > > > x is 50 > > Changed local x to 2 > > x is still 2 > > > > > > 2005/8/23, Deslay < deslay at gmail.com>: > > > 多谢各位. > > > missv > > > <lj0508 at gmail.com> to python-chinese More options 7:38 pm (1½ hours > ago) > > > > def func(x): > > > > print 'x is', x #这个x是函数参数x,值为50 > > > > x = 2 #这个x是函数内的x,作用域从这一句开始,并覆盖参数x > > > > print 'Changed local x to', x这个我想我看明白了的. > > > > > > > > > On 8/23/05, limodou < limodou at gmail.com> wrote: > > > > > > > > 在 05-8-23,Deslay<deslay at gmail.com> 写道: > > > > > hi all, > > > > > 我这里有个代码, > > > > > #!/usr/bin/python > > > > > def func(x): > > > > > print 'x is', x > > > > > x = 2 > > > > > print 'Changed local x to', x > > > > > > > > > > x = 50 > > > > > func(x) > > > > > print 'x is still', x > > > > > > > > > > 为什么输出会是 > > > > > x is 50 > > > > > Changed local x to 2 > > > > > x is still 50 > > > > > 为什么第一个x is 50用的会是function外的x = 50呢? > > > > > 而非x = 2? > > > > > > > > > > 请教!多谢! > > > > > > > > > > > > > Python中的参数是值参,而不是变参。因此在函数中的对参数的修改,并不影响外面的原值。 > > > > > > > > -- > > > > I like python! > > > > My Donews Blog: http://www.donews.net/limodou > > > > > > > > _______________________________________________ > > > > python-chinese list > > > > python-chinese at lists.python.cn > > > > http://python.cn/mailman/listinfo/python-chinese > > > > > > > > > > > > > > > > > > > > > > > > -- > > > ---------------------------------------------- > > > Name: Hugh Rex > > > Blog: http://www.hughr.org/ > > > Location: Huizhou CHINA > > > Matrix: MESS WITH THE BESS DIE LIKE THE REST > > > ---------------------------------------------- > > > _______________________________________________ > > > 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 > > > > > > > > > > -- > ############################################# > > # "your future depends on your dreams." so go to sleep. > # save water. shower with your girlfriend. > # > # Welcom2 : http//blog.csdn.net/auding1900/ > ############################################# > _______________________________________________ > python-chinese list > python-chinese at lists.python.cn > http://python.cn/mailman/listinfo/python-chinese > > >
2005年08月24日 星期三 11:17
On 8/24/05, D Shi Ming <dushmo at gmail.com> wrote: > 我使用的是Python 2.4.1 > > 2005/8/24, Spirit Auding <auding at gmail.com>: > > 阁下的Python什么版本?我的2.4,编译通不过,global不能这么用 > > > > > > On 8/24/05, D Shi Ming <dushmo at gmail.com > wrote: > > > 使用global语句,告诉Python这个变量名不是局部的,而是全局的。 > > > def func(x): > > > global x > > > print 'x is', x > > > x = 2 > > > print 'Changed local x to', x > > > > > > x=50 > > > func() > > > print 'x is still', x > > > > > > x is 50 > > > Changed local x to 2 > > > x is still 2 > > > > > > 不对啊, 根据定义, x在声明为函数参数之后是不能用在global语句里的,2.4.1的版本也没有改变 Names listed in a global statement must not be defined as formal parameters or in a for loop control target, class definition, function definition, or import statement. ---- http://www.python.org/doc/2.4.1/ref/global.html#l2h-557 -- simple is good
2005年08月24日 星期三 12:06
程序运行没有问题呀!! 在 05-8-24,Bruce Wang<number5 at gmail.com> 写道: > On 8/24/05, D Shi Ming <dushmo at gmail.com> wrote: > > 我使用的是Python 2.4.1 > > > > 2005/8/24, Spirit Auding <auding at gmail.com>: > > > 阁下的Python什么版本?我的2.4,编译通不过,global不能这么用 > > > > > > > > > On 8/24/05, D Shi Ming <dushmo at gmail.com > wrote: > > > > 使用global语句,告诉Python这个变量名不是局部的,而是全局的。 > > > > def func(x): > > > > global x > > > > print 'x is', x > > > > x = 2 > > > > print 'Changed local x to', x > > > > > > > > x=50 > > > > func() > > > > print 'x is still', x > > > > > > > > x is 50 > > > > Changed local x to 2 > > > > x is still 2 > > > > > > > > > > > 不对啊, 根据定义, x在声明为函数参数之后是不能用在global语句里的,2.4.1的版本也没有改变 > > Names listed in a global statement must not be defined as formal > parameters or in a for loop control target, class definition, function > definition, or import statement. > ---- http://www.python.org/doc/2.4.1/ref/global.html#l2h-557 > > -- > simple is good > > _______________________________________________ > python-chinese list > python-chinese at lists.python.cn > http://python.cn/mailman/listinfo/python-chinese > > >
2005年08月31日 星期三 17:38
>>> def func(x): global x SyntaxError: name 'x' is a function parameter and declared global (, line 1) >>> 不行啊 在 05-8-24,D Shi Ming<dushmo at gmail.com> 写道: > 程序运行没有问题呀!! > > 在 05-8-24,Bruce Wang<number5 at gmail.com> 写道: > > On 8/24/05, D Shi Ming <dushmo at gmail.com> wrote: > > > 我使用的是Python 2.4.1 > > > > > > 2005/8/24, Spirit Auding <auding at gmail.com>: > > > > 阁下的Python什么版本?我的2.4,编译通不过,global不能这么用 > > > > > > > > > > > > On 8/24/05, D Shi Ming <dushmo at gmail.com > wrote: > > > > > 使用global语句,告诉Python这个变量名不是局部的,而是全局的。 > > > > > def func(x): > > > > > global x > > > > > print 'x is', x > > > > > x = 2 > > > > > print 'Changed local x to', x > > > > > > > > > > x=50 > > > > > func() > > > > > print 'x is still', x > > > > > > > > > > x is 50 > > > > > Changed local x to 2 > > > > > x is still 2 > > > > > > > > > > > > > > > > 不对啊, 根据定义, x在声明为函数参数之后是不能用在global语句里的,2.4.1的版本也没有改变 > > > > Names listed in a global statement must not be defined as formal > > parameters or in a for loop control target, class definition, function > > definition, or import statement. > > ---- http://www.python.org/doc/2.4.1/ref/global.html#l2h-557 > > > > -- > > simple is good > > > > _______________________________________________ > > 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 > > >
Zeuux © 2025
京ICP备05028076号