Python论坛  - 讨论区

标题:[python-chinese] python 局部变量

2006年06月20日 星期二 22:20

crocodile.liu crocodile.liu at 163.com
Tue Jun 20 22:20:26 HKT 2006

def fun2():
a+=1
print a
def fun1():
a=1
fun2()
fun1()
我想让a作为fun1的局部变量,但实际运行起来确不行?


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

2006年06月20日 星期二 22:28

xxmplus xxmplus at gmail.com
Tue Jun 20 22:28:31 HKT 2006

a的确是fun1的局部变量,所以和fun2没关系,你要把它传给fun2才行

On 6/21/06, crocodile.liu <crocodile.liu at 163.com> wrote:
>
> def fun2():
> a+=1
> print a
> def fun1():
> a=1
> fun2()
> fun1()
> 我想让a作为fun1的局部变量,但实际运行起来确不行?
>
> _______________________________________________
> 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]

2006年06月20日 星期二 22:35

crocodile.liu crocodile.liu at 163.com
Tue Jun 20 22:35:18 HKT 2006

xxmplus 写道:
> a的确是fun1的局部变量,所以和fun2没关系,你要把它传给fun2才行
>
> On 6/21/06, crocodile.liu <crocodile.liu at 163.com> wrote:
>>
>> def fun2():
>> a+=1
>> print a
>> def fun1():
>> a=1
>> fun2()
>> fun1()
>> 我想让a作为fun1的局部变量,但实际运行起来确不行?
>>
>> _______________________________________________
>> 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



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

2006年06月20日 星期二 22:37

crocodile.liu crocodile.liu at 163.com
Tue Jun 20 22:37:25 HKT 2006

我想在运行fun2后修改a的值

crocodile.liu 写道:
> xxmplus 写道:
>   
>> a的确是fun1的局部变量,所以和fun2没关系,你要把它传给fun2才行
>>
>> On 6/21/06, crocodile.liu <crocodile.liu at 163.com> wrote:
>>     
>>> def fun2():
>>> a+=1
>>> print a
>>> def fun1():
>>> a=1
>>> fun2()
>>> fun1()
>>> 我想让a作为fun1的局部变量,但实际运行起来确不行?
>>>
>>> _______________________________________________
>>> 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
>>     
>
>
> _______________________________________________
> 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]

2006年06月21日 星期三 10:19

刘鑫 march.liu at gmail.com
Wed Jun 21 10:19:24 HKT 2006

什么是函数?函数就是为了隔离数据和行为,局部变量就是应该作为局部变量使用。为什么要挑战如此基本的设计规则?这种想法从根本上违背了代码封装,违反了结构化编程的基本原则。函数唯一的数据传递途径就应该是参数,如果你想要一个函数(不是类方法)使用一个外部的数据,就应该把它通过参数传递进来,不应该有第二个途径――在实际使用中,我们确实有机会用到全局变量,也有很多使用全局变量写出的好代码,但是你这样的想法实在是不应该出现在2006年的代码中。

在06-6-20,crocodile.liu <crocodile.liu at 163.com> 写道:
>
>
> def fun2():
> a+=1
> print a
> def fun1():
> a=1
> fun2()
> fun1()
> 我想让a作为fun1的局部变量,但实际运行起来确不行?
>
> _______________________________________________
> 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
>



-- 
欢迎访问:
http://blog.csdn.net/ccat

刘鑫
March.Liu
-------------- next part --------------
An HTML attachment was scrubbed...
URL: http://lists.exoweb.net/pipermail/python-chinese/attachments/20060621/9454a5a5/attachment.htm

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

2006年06月21日 星期三 10:19

jacob jacob at exoweb.net
Wed Jun 21 10:19:43 HKT 2006

crocodile.liu wrote:

>def fun2():
>a+=1
>print a
>def fun1():
>a=1
>fun2()
>fun1()
>我想让a作为fun1的局部变量,但实际运行起来确不行?
>
>_______________________________________________
>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]

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

    你的回复:

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

    Zeuux © 2025

    京ICP备05028076号