类的方法
1.静态方法
1 class Dog(object): 2 def __init__(self,name): 3 self.name = name 4 5 @staticmethod 6 def eat(self): 7 print("%s is eating %s" % (self.name,"apple")) 8 9 def talk(self):10 print("%s is talking"% self.name)11 12 ainemo=Dog("tom")13 ainemo.eat(ainemo)14 ainemo.talk()15 #只是名义上归类管理,实际上在静态方法里访问不了实例中的任何属性16 17 显示结果:18 tom is eating apple19 tom is talking
2.类方法
1 class Dog(object): 2 name = "jack" #只有类变量才能在“类方法”中起作用 3 def __init__(self,name): 4 self.name = name #实例变量不起作用 5 6 @classmethod 7 def eat(self): 8 print("%s is eating %s" % (self.name,"apple")) 9 10 def talk(self):11 print("%s is talking"% self.name)12 13 d=Dog("tom")14 d.eat()15 16 #只能访问类变量,不能访问实例变量17 显示结果:18 jack is eating apple
3.属性方法
1 class Dog(object): 2 def __init__(self,name): 3 self.name = name 4 self.__food = None 5 6 @property #attribute 7 def eat(self): 8 print("%s is eating %s" % (self.name,self.__food)) 9 10 @eat.setter11 def eat(self,food):12 print("food is %s" % food)13 #print("修改成功,%s is eating %s" % (self.name,food))14 self.__food = food15 16 @eat.deleter17 def eat(self):18 del self.__food19 print("删完了")20 21 def talk(self):22 print("%s is talking"% self.name)23 24 d=Dog("tom")25 d.eat26 d.eat="banana"27 d.eat28 #del d.eat #会报错,因为将self.__food整个变量删除了29 #d.eat 30 31 #把一个方法变成一个静态属性32 显示结果:33 tom is eating None34 food is banana35 tom is eating banana
4.反射
1.hasattr
1 class Dog(object): 2 def __init__(self,name): 3 self.name = name 4 5 def bulk(self): 6 print("%s is yelling....." % self.name) 7 8 def run(self): 9 print("%s is running....." % self.name)10 11 d = Dog("tom")12 choice = input(">>:").strip()13 print(hasattr(d,choice))14 15 显示结果:16 >>:run17 True
2.getattr
1 class Dog(object): 2 def __init__(self,name): 3 self.name = name 4 5 def bulk(self): 6 print("%s is yelling....." % self.name) 7 8 def run(self): 9 print("%s is running....." % self.name)10 11 d = Dog("tom")12 choice = input(">>:").strip()13 # print(hasattr(d,choice))14 #15 print(getattr(d,choice))16 getattr(d,choice)()17 18 显示结果:19 >>:run20>21 tom is running.....
3.setattr
1 def eat(self): 2 print("%s is eating....." %self.name) 3 4 class Dog(object): 5 def __init__(self,name): 6 self.name = name 7 8 def bulk(self): 9 print("%s is yelling....." % self.name)10 11 def run(self):12 print("%s is running....." % self.name)13 14 d = Dog("tom")15 choice = input(">>:").strip()16 # print(hasattr(d,choice))17 #18 # print(getattr(d,choice))19 # getattr(d,choice)()20 21 if hasattr(d,choice):22 # attr = getattr(d,choice)23 setattr(d,choice,"jack")24 print(getattr(d,choice))25 else:26 setattr(d,choice,eat)27 print(getattr(d,choice))28 d.eat(d)29 30 显示结果:31 >>:eat3233 tom is eating....
动态倒入模块
importlib
新建lib目录,边界aa.pyclass C(object): def __init__(self): self.name = "panjitao"切换到lib同级目录,新建test.pyimport importlibaa = importlib.import_module("lib.aa")print(aa.C().name)执行结果:panjitao
断言
assert
#断言成功import importlibaa = importlib.import_module("lib.aa")obj = aa.C().nameassert type(obj) is strprint("obj is str!")显示结果:obj is str!#断言失败import importlibaa = importlib.import_module("lib.aa")obj = aa.C().nameassert type(obj) is intprint("obj is int!")显示结果:Traceback (most recent call last): File "D:/Program Files/python-code/s12/day07/动态倒入模块.py", line 10, inassert type(obj) is intAssertionError