博客
关于我
强烈建议你试试无所不能的chatGPT,快点击我
第七节
阅读量:6800 次
发布时间:2019-06-26

本文共 3791 字,大约阅读时间需要 12 分钟。

类的方法

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
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 >>:run20 
>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 >>:eat32 
33 tom is eating....
setattr

动态倒入模块

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
importlib

断言

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, in 
assert type(obj) is intAssertionError
assert

 

 

 
 
 

转载于:https://www.cnblogs.com/ttyypjt/p/7291996.html

你可能感兴趣的文章
CentOS 7最小安装后,手动连接网络
查看>>
选择排序
查看>>
卷积神经网络(基础知识回顾)-第七讲
查看>>
inno setup中文支持
查看>>
js内存泄漏的问题?
查看>>
程序代码阅读与分析
查看>>
Linux 安装PHP PECL 百分百成功
查看>>
关于c++风格 code style
查看>>
svn 常用
查看>>
SVM支持向量机
查看>>
Asymptote 学习记录(2):例子阅读
查看>>
《常微分方程教程》习题2-2,4:一个跟踪问题
查看>>
陶哲轩实分析例17.2.3
查看>>
兩個集合之間的全體部分函數可以形成一個集合
查看>>
Elementary Methods in Number Theory Exercise 1.2.17
查看>>
认识拨号计划 - Dialplan
查看>>
DataTable 的数据导出到 Excel
查看>>
委托由浅入深学习
查看>>
BZOJ 1012 [JSOI2008]最大数maxnumber
查看>>
权限管理[Linux]
查看>>