| 网站首页 | 业界新闻 | 小组 | 威客 | 人才 | 下载频道 | 博客 | 代码贴 | 在线编程 | 编程论坛
欢迎加入我们,一同切磋技术
用户名:   
 
密 码:  
共有 6664 人关注过本帖
标题:python 例
取消只看楼主 加入收藏
madfrogme
Rank: 16Rank: 16Rank: 16Rank: 16
等 级:版主
威 望:21
帖 子:1160
专家分:1106
注 册:2009-6-24
结帖率:98.63%
收藏
 问题点数:0 回复次数:20 
python 例
说明局部变量的程序
程序代码:
#!/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

$ ./func_local.py
x is 50
Changed local x to 2
x is still 50

[ 本帖最后由 madfrogme 于 2012-8-30 21:10 编辑 ]
搜索更多相关主题的帖子: python local 
2012-08-30 17:15
madfrogme
Rank: 16Rank: 16Rank: 16Rank: 16
等 级:版主
威 望:21
帖 子:1160
专家分:1106
注 册:2009-6-24
收藏
得分:0 
如果你想要为一个定义在函数外的变量赋值,那么你就得告诉Python这个变量名不是局部的,而是 全局 的。我们使用global语句完成这一功能。
程序代码:
#!/usr/bin/python

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

$ ./func_global.py
x is 50
Changed local x to 2
Value of x is 2

注意:python没有接受x作为参数

[ 本帖最后由 madfrogme 于 2012-8-30 21:11 编辑 ]

The quieter you become, the more you can hear
2012-08-30 17:21
madfrogme
Rank: 16Rank: 16Rank: 16Rank: 16
等 级:版主
威 望:21
帖 子:1160
专家分:1106
注 册:2009-6-24
收藏
得分:0 
对于一些函数,你可能希望它的一些参数是 可选 的,

如果用户不想要为这些参数提供值的话,这些参数就使用默认值。
程序代码:
#!/usr/bin/python

def say( message, times = 1):
    print message * times
say('Hello')
say('World',3)


$ ./func_default.py
Hello
WorldWorldWorld

[ 本帖最后由 madfrogme 于 2012-8-30 21:10 编辑 ]

The quieter you become, the more you can hear
2012-08-30 19:17
madfrogme
Rank: 16Rank: 16Rank: 16Rank: 16
等 级:版主
威 望:21
帖 子:1160
专家分:1106
注 册:2009-6-24
收藏
得分:0 
我们不必担心参数的顺序,通过命名来为这些参数赋值——这被称作 关键参数 ——我们使用名字(关键字)而不是位置

程序代码:
#!/usr/bin/python
def func(a, b=5, c=10):
    print 'a is', a, 'and b is', b, 'and c is', c

func(3, 7)
func(25, c=24)
func(c=50, a=100)


$ ./func_key.py
a is 3 and b is 7 and c is 10
a is 25 and b is 5 and c is 24
a is 100 and b is 5 and c is 50

[ 本帖最后由 madfrogme 于 2012-8-30 21:10 编辑 ]

The quieter you become, the more you can hear
2012-08-30 19:57
madfrogme
Rank: 16Rank: 16Rank: 16Rank: 16
等 级:版主
威 望:21
帖 子:1160
专家分:1106
注 册:2009-6-24
收藏
得分:0 
除非你提供你自己的return语句,每个函数都在结尾暗含有return None语句。

def someFunction():
    pass

pass语句在Python中表示一个空的语句块.

[ 本帖最后由 madfrogme 于 2012-8-30 23:03 编辑 ]

The quieter you become, the more you can hear
2012-08-30 22:01
madfrogme
Rank: 16Rank: 16Rank: 16Rank: 16
等 级:版主
威 望:21
帖 子:1160
专家分:1106
注 册:2009-6-24
收藏
得分:0 
使用__doc__(注意双下划线)调用printMax函数的文档字符串属性

Python把 每一样东西 都作为对象,包括这个函数。

程序代码:
#!/usr/bin/python
def printMax(x, y):
    ''' Prints the maximum of two number.
    The two values must be integers.'''
    x = int(x)
    y = int(y)

    if x > y:
        print x, 'is maximum'
    else:
        print y, 'is maximum'

printMax(3,5)
print printMax.__doc__

$ ./func_doc.py
5 is maximum
 Prints the maximum of two number.
    The two values must be integers.

The quieter you become, the more you can hear
2012-08-30 22:15
madfrogme
Rank: 16Rank: 16Rank: 16Rank: 16
等 级:版主
威 望:21
帖 子:1160
专家分:1106
注 册:2009-6-24
收藏
得分:0 
使用__doc__(注意双下划线)调用printMax函数的文档字符串属性

Python把 每一样东西 都作为对象,包括这个函数。

程序代码:
#!/usr/bin/python
def printMax(x, y):
    ''' Prints the maximum of two number.
    The two values must be integers.'''
    x = int(x)
    y = int(y)

    if x > y:
        print x, 'is maximum'
    else:
        print y, 'is maximum'

printMax(3,5)
print printMax.__doc__

$ ./func_doc.py
5 is maximum
 Prints the maximum of two number.
    The two values must be integers.

The quieter you become, the more you can hear
2012-08-30 22:17
madfrogme
Rank: 16Rank: 16Rank: 16Rank: 16
等 级:版主
威 望:21
帖 子:1160
专家分:1106
注 册:2009-6-24
收藏
得分:0 
sys 标准库模块 sys.argv, sys.path
程序代码:
#!/usr/bin/python
import sys
print 'The command line arguments are:'
for i in sys.argv:
    print i

print '\n\nThe PYTHONPATH is', sys.path, '\n'



$ ./using_sys.py this is a test
The command line arguments are:
./using_sys.py
this
is
a
test

The PYTHONPATH is ['/Users/wzj', '/System/Library/Frameworks/Python.framework/Versions/2.7/lib/python27.zip', '/System/Library/Frameworks/Python.framework/Versions/2.7/lib/python2.7', '/System/Library/Frameworks/Python.framework/Versions/2.7/lib/python2.7/plat-darwin', '/System/Library/Frameworks/Python.framework/Versions/2.7/lib/python2.7/plat-mac', '/System/Library/Frameworks/Python.framework/Versions/2.7/lib/python2.7/plat-mac/lib-scriptpackages', '/System/Library/Frameworks/Python.framework/Versions/2.7/Extras/lib/python', '/System/Library/Frameworks/Python.framework/Versions/2.7/lib/python2.7/lib-tk', '/System/Library/Frameworks/Python.framework/Versions/2.7/lib/python2.7/lib-old', '/System/Library/Frameworks/Python.framework/Versions/2.7/lib/python2.7/lib-dynload', '/System/Library/Frameworks/Python.framework/Versions/2.7/Extras/lib/python/PyObjC', '/Library/Python/2.7/site-packages']

The quieter you become, the more you can hear
2012-08-31 23:28
madfrogme
Rank: 16Rank: 16Rank: 16Rank: 16
等 级:版主
威 望:21
帖 子:1160
专家分:1106
注 册:2009-6-24
收藏
得分:0 
那版主等我把楼盖上去了之后可以帮我移到python版啊,现在主要还是自己的学习阶段

The quieter you become, the more you can hear
2012-09-01 06:20
madfrogme
Rank: 16Rank: 16Rank: 16Rank: 16
等 级:版主
威 望:21
帖 子:1160
专家分:1106
注 册:2009-6-24
收藏
得分:0 
每个Python模块都有它的__name__ 属性,如果它是'__main__',这说明这个模块被用户单独运行,我们可以进行相应的恰当操作.

程序代码:
#!/usr/bin/python
# Filename: using_name.py

if __name__ == '__main__':
    print 'This program is being run by itself'
else:
    print 'I am being imported from another module'


[ 本帖最后由 madfrogme 于 2012-9-2 12:40 编辑 ]

The quieter you become, the more you can hear
2012-09-01 11:28
快速回复:python 例
数据加载中...
 
   



关于我们 | 广告合作 | 编程中国 | 清除Cookies | TOP | 手机版

编程中国 版权所有,并保留所有权利。
Powered by Discuz, Processed in 0.016576 second(s), 8 queries.
Copyright©2004-2024, BCCN.NET, All Rights Reserved