文章详情

  • 游戏榜单
  • 软件榜单
关闭导航
热搜榜
热门下载
热门标签
php爱好者> php文档>python的返回值

python的返回值

时间:2008-05-16  来源:yk325

    今天在试验<<PYTHON编程金典>>上的例程,发现了一个小问题,列举如下:
 
下面是创建的一个类

#! /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。
相关阅读 更多 +
排行榜 更多 +
找茬脑洞的世界安卓版

找茬脑洞的世界安卓版

休闲益智 下载
滑板英雄跑酷2手游

滑板英雄跑酷2手游

休闲益智 下载
披萨对对看下载

披萨对对看下载

休闲益智 下载