1.类方法与静态方法
class Date(object):
def __init__(self, year, month, day):
self.year = year
self.month = month
self.day = day
def echo(self):
return "%s %s %s" %(self.year, self.month, self.day)
@classmethod
def as_string(cls, s):
print(cls)
month, day, year = s.split('/')
d = cls(year, month, day)
return d
@staticmethod
def is_vaild(s):
month, day, year = map(int, s.split('/'))
return 0 < month <= 12 and 0 < day <= 31 and 1 < year < 9999
s = '10/10/2018'
print(Date.is_vaild(s))
d = Date(2018, 10, 10)
print(d.is_vaild('13/10/2019'))
from datetime import date
from datetime import date
2.with语句
class MyOpen(object):
def __init__(self, filename, mode='r'):
self._name = filename
self._mode = mode
def __enter__(self):
self.f = open(self._name, self._mode)
return self.f
def __exit__(self, exc_type, exc_val, exc_tb):
self.f.close()
@property
def name(self):
return self._name
@property
def mode(self):
return self._mode
with MyOpen('/etc/passwd') as f:
print(f.closed)
print(f.read(5))
print(f.closed)
3.面向对象的反射机制
反射: 让对象告诉我们相关信息(对象拥有的属性和方法, 对象所属的类等....)
print(dir(str))
f = open('/tmp/passwd')
print(dir(f))
print(type('hello'))
class Student(object):
"""
这是student类的帮助文档
"""
def __init__(self, name, age):
self.name = name
self.__age = age
def get_score(self):
return "score"
def get_grade(self):
return 'grade'
s1 = Student("fentiao", 10)
print(type(s1))
print(isinstance(s1, Student))
print(isinstance('hello', Student))
print(s1.__class__)
print(s1.__dict__)
print(s1.__doc__)
print(hasattr(s1, 'name'))
print(hasattr(s1, '__age'))
print(hasattr(s1, 'score'))
print(hasattr(s1, 'get_score'))
print(hasattr(s1, 'set_score'))
print(getattr(s1, 'name'))
print(getattr(s1, '__age', 'no attr'))
print(getattr(s1, 'get_score', 'no method'))
print(getattr(s1, 'set_score', 'no method'))
setattr(s1, 'name', 'westos')
print(getattr(s1, 'name'))
setattr(s1, 'score', 100)
print(getattr(s1, 'score'))
def get_score1():
return "这是修改的方法内容"
setattr(s1, 'get_score', get_score1)
print(getattr(s1, 'get_score')())
def set_score():
return "这是添加的方法"
setattr(s1, 'set_score', set_score)
print(getattr(s1, 'set_score')())
delattr(s1, 'name')
print(hasattr(s1, 'name'))
print(hasattr(s1, 'set_score'))
delattr(s1, 'set_score')
print(hasattr(s1, 'set_score'))
4.反射机制的应用场景_动态方法调用
https://www.csdn.net/nav/newarticles
https://www.csdn.net/nav/watchers
https://www.csdn.net/nav/news
https://www.csdn.net/nav/ai
https://www.csdn.net/bbs/newarticles
https://www.csdn.net/bbs/watchers
https://www.csdn.net/bbs/news
https://www.csdn.net/bbs/ai
class Web(object):
def newarticles(self):
return "<h1>newarticles</h1>"
def watchers(self):
return "<h1>watchers</h1>"
def news(self):
return "<h1>news</h1>"
def ai(self):
return "<h1>ai</h1>"
flask = Web()
def run():
url = input("url:").split('/')[-1]
if hasattr(flask, url):
return getattr(flask, url)()
else:
return "<h1>404</h1>"
if __name__ == "__main__":
while True:
print(run())
5.反射机制与动态倒入模块
/bbs/login
/bbs/index
/blog/login
/blog/index
def run():
modules, func = input("url:").split('/')[-2:]
obj = __import__('lib.'+ modules)
print(obj)
if hasattr(obj, func):
fun = getattr(obj, func)
return fun()
else:
return "404: 页面找不到"
if __name__ == "__main__":
while True:
print(run())
6.倒入模块import理解
import lib.bbs as obj
print(obj.index())
obj = __import__('lib.bbs')
print(obj.index())