免费注册 查看新帖 |

Chinaunix

  平台 论坛 博客 文库
最近访问板块 发新帖
查看: 2127 | 回复: 0
打印 上一主题 下一主题

python的返回值 [复制链接]

论坛徽章:
0
跳转到指定楼层
1 [收藏(0)] [报告]
发表于 2008-05-16 21:36 |只看该作者 |倒序浏览

                    今天在试验>上的例程,发现了一个小问题,列举如下:

下面是创建的一个类
#! /usr/bin/python
# FileName:    time1.py
# Description: Simple definition of class Time
# Author:      yk
# Date:        Sat May 17 04:07:34 CST 2008
class Time:
    """Time abstract definition of class Time."""
    def __init__( self ):
        """Initializes hour,minute and second to zero"""
        self.hour = 0
        self.minute = 0
        self.second = 0
    def printMilitary( self ):
        """Prints object of class Time in military format"""
        
        print "%.2d:%.2d:%.2d" % ( self.hour,self.minute,self.second )
    def printStandard( self ):
        """Prints object of class Time in standard format"""
        standardTime = ""
        if self.hour == 0 or self.hour == 12:
            standardTime += "12:"
        else:
            standardTime += "%d:" % ( self.hour )
        standardTime += "%.2d:%.2d" % ( self.minute,self.second )
        if self.hour  12:
            standardTime += "AM"
        else:
            standardTime += "PM"
        print standardTime
下面是测试这个类的一个小程序
#! /usr/bin/python
# FileName:    t702.py
# Description: Creating and manipulating objects of class Time.
# Author:      yk
# Date:        Sat May 17 04:25:03 CST 2008
from time1 import Time
time = Time()
print "The attributes of time are: "
print "time1.hour:",time.hour
print "time1.minute:",time.minute
print "time1.second:",time.second
print "\nCalling method printMilitary:",
time.printMilitary()
print "\nCalling method printStandard:",
time.printStandard()
print "\n\nChanging time1's hour attribute..."
time.hour = 25
print "Calling method printMilitary after alteration:",
time.printMilitary()
当程序如上所示的时候,程序的输出为:
The attributes of time are:
time1.hour: 0
time1.minute: 0
time1.second: 0
Calling method printMilitary: 00:00:00
Calling method printStandard: 12:00:00AM
Changing time1
但是,如果把类的函数放在print语句的同一行的时候,如把下边的语句
print "\nCalling method printMilitary:",
time.printMilitary()
改为
print "\nCalling method printMilitary:",time.printMilitary()

后,程序的输出如下所示:
The attributes of time are:
time1.hour: 0
time1.minute: 0
time1.second: 0
Calling method printMilitary: 00:00:00
None
Calling method printStandard: 12:00:00AM
None
Changing time1's hour attribute...
Calling method printMilitary after alteration: 25:00:00
None
    在网上查了一下,有如下的说明,在PYTHON中,函数没有定义返回的数据类型。Python 不需要指定返回值的数据类型;甚至不需要指定是否有返回值。实际上,每个 Python 函数都返回一个值;如果函数执行过 return 语句,它将返回指定的值,否则将返回 None (Python 的空值)
   
    但是,我现在还是不明白,为什么换行的话就不输出None。
               
               

本文来自ChinaUnix博客,如果查看原文请点:http://blog.chinaunix.net/u1/50916/showart_691145.html
您需要登录后才可以回帖 登录 | 注册

本版积分规则 发表回复

  

北京盛拓优讯信息技术有限公司. 版权所有 京ICP备16024965号-6 北京市公安局海淀分局网监中心备案编号:11010802020122 niuxiaotong@pcpop.com 17352615567
未成年举报专区
中国互联网协会会员  联系我们:huangweiwei@itpub.net
感谢所有关心和支持过ChinaUnix的朋友们 转载本站内容请注明原作者名及出处

清除 Cookies - ChinaUnix - Archiver - WAP - TOP