第七节

类的方法

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 apple
19 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.setter
11     def eat(self,food):
12         print("food is %s" % food)
13         #print("修改成功,%s is eating %s" % (self.name,food))
14         self.__food = food
15 
16     @eat.deleter
17     def eat(self):
18         del self.__food
19         print("删完了")
20 
21     def talk(self):
22         print("%s is talking"% self.name)
23 
24 d=Dog("tom")
25 d.eat
26 d.eat="banana"
27 d.eat
28 #del d.eat  #会报错,因为将self.__food整个变量删除了
29 #d.eat   
30 
31 #把一个方法变成一个静态属性
32 显示结果:
33 tom is eating None
34 food is banana
35 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 >>:run
17 True
hasattr

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 >>:run
20 <bound method Dog.run of <__main__.Dog object at 0x000000B2BFDC98D0>>
21 tom is running.....
getattr

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 >>:eat
32 <function eat at 0x000000C37E183E18>
33 tom is eating....
setattr

动态倒入模块

importlib

新建lib目录,边界aa.py
class C(object):
    def __init__(self):
        self.name = "panjitao"
切换到lib同级目录,新建test.py
import importlib

aa = importlib.import_module("lib.aa")
print(aa.C().name)

执行结果:
panjitao
importlib

断言

assert

#断言成功
import importlib

aa = importlib.import_module("lib.aa")

obj = aa.C().name
assert type(obj) is str
print("obj is str!")

显示结果:
obj is str!

#断言失败
import importlib

aa = importlib.import_module("lib.aa")
obj = aa.C().name

assert type(obj) is int
print("obj is int!")
显示结果:
Traceback (most recent call last):
  File "D:/Program Files/python-code/s12/day07/动态倒入模块.py", line 10, in <module>
    assert type(obj) is int
AssertionError
assert







原文地址:https://www.cnblogs.com/ttyypjt/p/7291996.html