def addMe2Me(x):
return (x+x)
print addMe2Me(4.25)
print addMe2Me(10)
print addMe2Me('python')
def foo(debug=True):
if debug:
print 'in debug mode'
else:
print 'not in debug mode'
foo()
#define a class and test it
class people:
m_name = ''
m_nAge=0
m_nWeight=0
def __init__(self,name,age,weight):
self.m_name = name
self.m_nAge = age
self.m_nWeight = weight
def __del__(self):
print("destructrue called")
def speak(self):
print("%s is speaking:I am %d years old" % (self.m_name,self.m_nAge))
pObj = people('jinwei',23,75)
pObj.speak()
#单继承测试程序
class student(people):
grade = ''
def __init__(self,name,age,weight,grade):
people.__init__(self,name,age,weight)
self.grade = grade
def speak(self):
print("%s is speaking:I am %d years old,and I am in grade %d" % (self.m_name,self.m_nAge,self.grade))
sObj = student('bloodchilde',23,75,6)
sObj.speak()
#多重继承测试案例
class speaker:
name=''
topic=''
def __init__(self,name,topic):
self.name=name
self.topic=topic
def speak(self):
print("I am %s,I am a speaker! my topic is %s" % (self.name,self.topic))
class multiSample(speaker,student):
a=''
def __init__(self,name,age,weight,grade,topic):
student.__init__(self,name,age,weight,grade)
speaker.__init__(self,name,topic)
multiObj = multiSample('xuegonzi',24,75,6,'python')
multiObj.speak()
#函数测试程序
def hello():
print 'hello world'
hello()
l=[1,2,3,4]
def sum(List):
result = 0
for i in List:
result = result+i
return result
print(sum(l))
python 类定义
最新推荐文章于 2023-03-13 16:17:32 发布