#!/usr/bin/env python3#coding=utf-8#Version:python3.6.1#Project:practice#File:Python就业班——初识面向对象.py#Data:2020/7/10 16:15#Author:LGSP_Harold
#判断一个对象是否是一个已知的类型#isinstance(obj, class)#isinstance(22, int)#判断是否为子类#issubclass(Cat, BaseCat) True or False
#————————————————————————————#@property:将类的方法当做属性来使用#class Cat(object):#
#def __init__(self, name, age):#self.name = name#self.__age = age#
#@property#def age(self):#return self.__age#
#@age.setter#def age(self, value):#if not isinstance(value, int):#print('年龄只能是整数')#return 0#if not 0 <= value <= 100:#print('年龄只能介于0-100之间')#return 0#self.__age = value#
#@property#def show_info(self):#return '我叫{0},今年{1}岁'.format(self.name, self.age)
#————————————————————————————#__slots__:为指定的类设置一个静态属性列表,为属性很少的类节约内存空间#class Cat(object):#__slots__ = ('name', 'age')#
#def __init__(self, name, age):#self.name = name#self.age = age#
#@property#def show_info(self):#return '{0},{1}'.format(self.name, self.age)#
#def __str__(self):#return '我的对象:{0}'.format(self.name)#
#
#def eat():#print('我吃鱼')#
#
#class DuanCat(Cat):#"""#蓝短猫#"""#pass#
#
#class PersianCat(Cat):#"""#波斯猫#"""#__slots__ = ('sex', 'eat', )#pass
#————————————————————————————#类的静态方法和实例方法#@staticmethod:表示静态方法#@classmethod:类的方法
#class Cat(object):#
#tag = '猫科'#
#def __init__(self, name):#self.name = name#
#@staticmethod#def breath():#print('呼吸')#
#def show_info(self):#print('类的属性:{0},实例的属性:{1}'.format(self.tag, self.name))#
## @classmethod## def show_info1(cls):## # cls.name ==> Cat.name## print('类的属性:{0},实例的属性:{1}'.format(cls.tag, cls.name))#
#@classmethod#def show_info2(cls, name):## cls.name ==> Cat.name## print('类的属性:{0},实例的属性:{1}'.format(cls.tag, cls.name))#return cls(name)
#————————————————————————————#装饰器#用来拓展原来函数功能的一种函数#返回函数的函数#在不用更改原函数的代码前提下给函数增加新的功能#def log(func):#def wrapper():#print('start....')#func()#print('end....')#return wrapper#
#
#@log#def hello():#print('hello')
#带参数的装饰器#def log(name=None):#def decorator(func):#def wrapper():#print('{0}Start....'.format(name))#func()#print('{0}End....'.format(name))#return wrapper#return decorator#
#
#@log()#@log('you')#def hello():#print('hello')#
#
#def log(name=None):#def decorator(func):#def wrapper(*args, **kwargs):#print('{0}Start....'.format(name))#print(args)#print(kwargs)#rest = func(*args, **kwargs)#print('{0}End....'.format(name))#return rest#return wrapper#return decorator#
#
#@log('from add')#def add(a, b, *args, **kwargs):#return a + b
#def log(func):#def wrapper(*args, **kwargs):#rest = func(*args, **kwargs)#return rest#return wrapper#
#
#@log#def add(*args, **kwargs):#j = 0#for item in args:#j += item#return j
## @wraps(func)用法#def log(name=None):#def decorator(func):#@wraps(func)#def wrapper(a, b, *args, **kwargs):#""" 装饰器内部的函数 """#print('Start,{0}'.format(name))#print('{0}{1}'.format(func.__name__,func.__doc__))#num = func(a, b, *args, **kwargs)#print('End')#return num#print('{0}{1}'.format(func.__name__, func.__doc__))#return wrapper#return decorator#
#
#@log('Leslie')#def add(a, b, *args, **kwargs):#""" 加法计算 """#return a + b
## 类的装饰器#def func(self):#print('{0}我要吃东西【func()】'.format(self.name))#
#
#def eat(cls):#""" 吃东西装饰器 """#cls.eat = lambda self: print('{0}我要吃东西【eat()】'.format(self.name))#cls.eat1 = func#return cls#
#
#@eat#class Dog(object):#""" 狗类 """#def __init__(self, name):#self.name = name
#————————————————————————————#迭代器#1、迭代(iterate)意味着重复多次,就像循环那样(list,tuple)#2、实现了方法__iter__的对象是可迭代的,而实现了方法__next__的对象是迭代器#3、调用方法__next__时,(或next()),迭代器返回其下一个值#4、如果迭代器没有可供返回的值,触发StopIteration异常
#从迭代器创建序列#class PowNumber(object):#"""#迭代器#生成1,2,3,....数的平方#"""#value = 0#def __next__(self):#self.value += 1#if self.value > 20:#raise StopIteration#return self.value * self.value#
#def __iter__(self):#return self
#————————————————————————————#生成器#生成器是一种使用普通函数语法定义的迭代器#包含yield语句的函数都被称为生成器#不使用return返回一个值,而是可以生成多个值,每次一个#每次使用yield生成一个值后,函数都将冻结,即在此停止执行#被重新唤醒后,函数将从停止的的地方开始继续执行#def pow():#yield 1#yield 2#yield 3#yield 4#yield 5#yield 6#
#
#def pow_number():#return (x * x for x in [1, 2, 3, 4, 5])#
#
#def pow_number1():#for x in [1, 2, 3, 4, 5]:#yield x * x
#模拟range函数## 迭代器#class IterRange(object):#
#def __init__(self, start, end):#self.start = start - 1#self.end = end#
#def __next__(self):#self.start += 1#if self.start >= self.end:#raise StopIteration#return self.start#
#def __iter__(self):#return self#
#
## 生成器#class GenRange(object):#
#def __init__(self, start, end):#self.start = start#self.end = end#
#def get_num(self):#while True:#if self.start >= self.end:#break#yield self.start#self.start += 1#
#
#def imitate_range(start, end):#while True:#if start >= end:#break#yield start#start += 1
#————————————————————————————#异常捕获#try:#pass#except Exception as err:#print(err)#finally:#pass
#自定义异常#class ApiException(Exception):#err_code = ''#err_msg = ''#def __init__(self, err_code=None, err_msg=None):#self.err_code = self.err_code if self.err_code else err_code#self.err_msg = self.err_msg if self.err_msg else err_msg#def __str__(self):#return 'Error{0}-{1}'.format(self.err_code, self.err_msg)#
#
#class InvalidCtrlExec(ApiException):#err_code = '40001'#err_msg = '不合法调用凭证'#
#
#class BadPramsException(ApiException):#err_code = '40002'#err_msg = '两个参数必须都是整数'#
#
#
#
#def divide(num1, num2):#if not isinstance(num1, int) or not isinstance(num2, int):#raise BadPramsException()#if num2 == 0:#raise ApiException('40000', '除数不能为0')#return num1 / num2
## 抛出异常及异常的传递#class MyException(Exception):#pass#
#
#def v_for():#""" 自定义函数 """#for i in range(1, 100):#if i == 20:#raise MyException#print(i)#
#
#def call_v_for():#""" 调用v_for函数 """#print("开始调用")#v_for()#print("结束调用")#
#
#def test_rasie():#print("测试函数")#try:#call_v_for()#except MyException:#print('____________________________')#print("测试完毕")
if __name__ == '__main__':#cat = Cat('XX', 10)
#rest = cat.show_info
#print(rest)
#cat.age = 10
#rest = cat.show_info
#print(rest)
#————————————————————————————
#cat_b = Cat('xx', 12)
#rest = cat_b.show_info
#print(rest)
## 给实例添加新的属性
#cat_b.color = 'while'
#print(cat_b.color)
## 给实例添加新的方法
#cat_b.eat = eat
#cat_b.eat()
#使用slots后不允许给实例添加新的属性
#cat_b.color = 'black'
#print(cat_b.color)
#使用slots后不允许给实例添加新的方法
#cat_b.eat = eat
#cat_b.eat()
## 使用__slots__要注意,__slots__定义的属性仅对当前类起作用,对继承的子类是不起作用的:(视频此处总结有误)
#duan_cat = DuanCat('oo', 15)
#rest = duan_cat.show_info
#print(rest)
#duan_cat.color = 'blue'
#print(duan_cat.color)
## 在子类中也定义__slots__,这样,子类允许定义的属性就是自身的__slots__加上父类的__slots__。
#persian_cat = PersianCat('hh', 0)
#rest = persian_cat.show_info
#print(rest)
#persian_cat.sex = '母'
#print(persian_cat.sex)
#persian_cat.eat = eat
#persian_cat.eat()
#persian_cat.color = 'while'
#print(persian_cat.color)
#————————————————————————————
## 通过类进行调用
#Cat.breath()
#cat = Cat('小黑')
## 通过类的实例化进行调用
#cat.breath()
#cat.show_info()
## cat.show_info1()
#此处视频内容不知是太难了还是讲师只是讲个大概(类方法),反正我没听懂,建议看文档自行理解。
#调用classmethod
#cat2 = Cat.show_info2('oo')
#cat2.show_info()
#————————————————————————————
#hello()
#hello()
#rest = add(5, 6)
#print(rest)
#rest = add(5, 6)
#print(rest)
#rest = add(5, 6)
#print('{0}{1}'.format(add.__name__, add.__doc__))
#print(rest)
## 类的装饰器
#dog = Dog('小黑')
#dog.eat()
#dog.eat1()
#————————————————————————————
#迭代器
#pow = PowNumber()
#print(pow.__next__())
#print(pow.__next__())
#print(next(pow))
#print(next(pow))
#for i in pow:
#print(i)
#————————————————————————————
#生成器
#rest = pow()
#print(next(rest))
#print(next(rest))
#print(next(rest))
#print('__________________')
#for i in rest:
#print(i)
#print('__________________')
#rest1 = pow_number1()
#print(rest1.__next__())
#print(next(rest1))
#————————————————————————————
#模拟range函数
#iter = IterRange(5, 10)
#print(list(iter))
# #gen = GenRange(5, 10).get_num()
#print(list(gen))
# #imitate = imitate_range(5, 10)
#print(list(imitate))
#————————————————————————————
#自定义异常
#try:
#rest = divide(4, 0)
## rest = divide(4, "00")
#print(rest)
# #except ApiException as err:
#print('出错了')
#print(err)
#test_rasie()
pass