Chinaunix首页 | 论坛 | 博客
  • 博客访问: 533216
  • 博文数量: 174
  • 博客积分: 4177
  • 博客等级: 上校
  • 技术积分: 1827
  • 用 户 组: 普通用户
  • 注册时间: 2007-10-15 14:12
文章分类

全部博文(174)

文章存档

2018年(1)

2017年(1)

2013年(3)

2012年(9)

2010年(12)

2009年(5)

2008年(106)

2007年(37)

我的朋友

分类: Python/Ruby

2008-05-16 21:36:12

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

#! /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。
阅读(2061) | 评论(0) | 转发(0) |
0

上一篇:python语法高亮设置

下一篇:Lambda表达式

给主人留下些什么吧!~~