类对象与继承
class A():
x =6
x1=["221"]
print("123")
def add(self,x,y):
return x+y
def __init__(self,m,n):
self.m =m
self.n = n
def fun(self,y):
print(y,self.x,self.m)
class B(A):
print("456")
x=63
def __init__(self,m,n,g):
super().__init__(m,n)
self.g=g
def add(self,m,n,g):
return m+n+g
a =B(9,52,6)
a.x =111
a.x1.append("3")
l =a.add(1,2,3)
t =a.x
k=a.m
print(l,t,a.m ,a.x1 is A.x1,a.x1,a.n,a.g)
返回值:
123
456
6 111 9 True ['221', '3'] 52 6
Process finished with exit code 0
异常与捕获
def f(a):
print(a+1)
try:
f(b)
except TypeError:
print("类型错误")
except NameError as e:
print("名称错误",e)
except Exception:
print("未知错误")
else:
print("正常")
finally:
print("all pass")
返回值
名称错误 name 'b' is not defined
all pass