#!/usr/bin/python
# Filename: inherit.py
class SchoolMember:
'''Represents any school member.'''
def __init__(self, name, age):
self.name = name
self.age = age
print('(Initialized SchoolMember: %s)' % self.name)
def tell(self):
'''Tell my details.'''
print('Name:"%s" Age:"%s"'% (self.name, self.age),)
class Teacher(SchoolMember):
'''Represents a teacher.'''
def __init__(self, name, age, salary):
SchoolMember.__init__(self, name, age)
self.salary = salary
print('(Initialized Teacher: %s)'% self.name)
def tell(self):
SchoolMember.tell(self)
print('Salary: "%d"'% self.salary)
class Student(SchoolMember):
'''Represents a student.'''
def __init__(self, name, age, marks):
SchoolMember.__init__(self, name, age)
self.marks = marks
print('(Initialized Student: %s)'% self.name)
def tell(self):
SchoolMember.tell(self)
print('Marks: "%d"'% self.marks)
t = Teacher('Mrs. Shrividya',40,30000)
s = Student('Swaroop',22,75)
members = [t, s]
for member in members:
# Filename: inherit.py
class SchoolMember:
'''Represents any school member.'''
def __init__(self, name, age):
self.name = name
self.age = age
print('(Initialized SchoolMember: %s)' % self.name)
def tell(self):
'''Tell my details.'''
print('Name:"%s" Age:"%s"'% (self.name, self.age),)
class Teacher(SchoolMember):
'''Represents a teacher.'''
def __init__(self, name, age, salary):
SchoolMember.__init__(self, name, age)
self.salary = salary
print('(Initialized Teacher: %s)'% self.name)
def tell(self):
SchoolMember.tell(self)
print('Salary: "%d"'% self.salary)
class Student(SchoolMember):
'''Represents a student.'''
def __init__(self, name, age, marks):
SchoolMember.__init__(self, name, age)
self.marks = marks
print('(Initialized Student: %s)'% self.name)
def tell(self):
SchoolMember.tell(self)
print('Marks: "%d"'% self.marks)
t = Teacher('Mrs. Shrividya',40,30000)
s = Student('Swaroop',22,75)
members = [t, s]
for member in members:
member.tell()# works for both Teachers and Students
为了使用继承,我们把基本类的名称作为一个元组跟在定义类时的类名称之后。然后,我们注意到基本类的__init__方法专门使用self变量调用,这样我们就可以初始化对象的基本类部分。这一点十分重要——Python不会自动调用基本类的constructor,你得亲自专门调用它。
我们还观察到我们在方法调用之前加上类名称前缀,然后把self变量及其他参数传递给它。
注意,在我们使用SchoolMember类的tell方法的时候,我们把Teacher和Student的实例仅仅作为SchoolMember的实例。
另外,在这个例子中,我们调用了子类型的tell方法,而不是SchoolMember类的tell方法。可以这样来理解,Python总是首先查找对应类型的方法,在这个例子中就是如此。如果它不能在导出类中找到对应的方法,它才开始到基本类中逐个查找。基本类是在类定义的时候,在元组之中指明的。
一个术语的注释——如果在继承元组中列了一个以上的类,那么它就被称作多重继承。
1. 输出字符串
>>> strHello = 'Hello World'
>>> print (strHello)
Hello World
2. 格式化输出整数
支持参数格式化,与C语言的printf类似
>>> strHello = "the length of (%s) is %d" %('Hello World',len('Hello World'))
>>> print (strHello)
the length of (Hello World) is 11
3. 格式化输出16进制,十进制,八进制整数
#%x --- hex 十六进制
#%d --- dec 十进制
#%o --- oct 八进制
>>> nHex = 0xFF
>>> print("nHex = %x,nDec = %d,nOct = %o" %(nHex,nHex,nHex))
nHex = ff,nDec = 255,nOct = 377
4.格式化输出浮点数(float)
import math
>>> print('PI=%f'%math.pi)
PI=3.141593
>>> print ("PI = %10.3f" % math.pi)
PI = 3.142
>>> print ("PI = %-10.3f" % math.pi)
PI = 3.142
>>> print ("PI = %06d" % int(math.pi))
PI = 000003
5. 格式化输出浮点数(float)
>>> precise = 3
>>> print ("%.3s " % ("python"))
pyt
>>> precise = 4
>>> print ("%.*s" % (4,"python"))
pyth
>>> print ("%10.3s " % ("python"))
pyt
6.输出列表(List)
输出列表
>>> lst = [1,2,3,4,'python']
>>> print (lst)
[1, 2, 3, 4, 'python']
输出字典
>>> d = {1:'A',2:'B',3:'C',4:'D'}
>>> print(d)
{1: 'A', 2: 'B', 3: 'C', 4: 'D'}
>>> print(d)
{1: 'A', 2: 'B', 3: 'C', 4: 'D'}
7. 自动换行
print 会自动在行末加上回车,如果不需回车,只需在print语句的结尾添加一个逗号”,“,就可以改变它的行为。
>>> for i in range(0,6):
print (i,)
0
1
2
3
4
5
或直接使用下面的函数进行输出:
>>> import sys
>>> sys.stdout.write('Hello World')
Hello World