# -*- coding: utf-8 -*-
"""
Created on Thu Apr 29 09:10:35 2021
@author: yongj
"""
import sys
import pandas as pd
import numpy as np
from functools import reduce
Digital = {"0":0,"1":1,"2":2,"3":3,"4":4,"5":5,"6":6,"7":7,"8":8,"9":9}
#map处理到问题的最小成员,reduce按规律重新组合最小成员
def str2num(str):
return reduce(lambda x, y: 10 *x +y, map(lambda s: Digital[s], str))
class Student(object):
def __init__(self, a, b):
self.a = a
self.b = b
#__slots__ = ("chance", "name", "gender") '''confine the attrbutes of class,including functions and variety'''
def setname(self, name):
self.name = name
def setage(self, age):
self.__age = age
def getname(self):
return self.name
def getage(self):
return self.__age
'''类中__xxx__方法支持外部对对象的调用'''
def __len__(self):
return 1
'''turn methodical method to property'''
@property
def score(self):
return self.__score
@score.setter
def score(self, score):
self.__score = score
'''return and cite __str__ when construct the object'''
def __str__(self):
return "str function vote when construct"
'''configure one class iterable attributes'''
def __iter__(self):
return self #class is iterable
def __next__(self):
self.a, self.b = self.b, self.a + self.b # 计算下一个值
if self.a > 100000: # 退出循环的条件
raise StopIteration()
return self.a # 返回下一个值
def __getitem__(self, nvalue): #slice operation and []
if isinstance(nvalue, int):
a,b = 1,1
for i in range(nvalue):
a,b = b, a+b
return a
if isinstance(nvalue, slice):
begin = nvalue.start
end = nvalue.stop
if nvalue is None:
begin = 0
a,b = 1,1
L = []
for x in range(end):
if x >= begin:
L.append(a)
a, b = b, a + b
return L
#return one atribute in dynamic way
def __getattr__(self, des):
if des == "des":
return "description"
#cite __call__ when object is calling
def __call__(self):
return "__call__ cited"
if __name__ == "__main__":
'''使用reduce、map的思维过程:一个任务先分解到最小处理,再重新组合'''
if isinstance(str2num("7897"), int):
print(str2num("8967"))
'''filter、map、reduce、sorted函数(传入的值均可为tuple list dict set)'''
#传入列表
print(list(filter(lambda num : num % 2 == 1, range(0,20))))
#传入tuple
print(list(filter(lambda num : num % 2 == 1, tuple(range(0,20)))))
#传入set集合数据类型
print(list(filter(lambda num : num % 2 == 1, set(range(0,20)))))
#传入dict默认为对key的操作,可用于操作操作字典的key
print(list(filter(lambda char: Digital[char] < 6, Digital)))
'''打印一个类全部的方法'''
print(dir(int))
print(dir(Student))
s1 = Student(0,1)
s1.setname("Crazyman")
print(len(s1))
'''判断对象中是否有某个属性,直接通过对象设定对象相关属性 getattr() 与 .方法一样'''
if hasattr(s1, 'name'):
print(getattr(s1, "name"))
setattr(s1, "name", "Fuckman")
print(getattr(s1, "name"))
#直接获取对象的属性
print(s1.name)
'''关于python面向对象的类属性与实例属性'''
s1.gender = "woman" #实例属性
print(dir(s1)) #dir()能够显示类属性与实例属性全部
print(set(dir(s1)).difference(set(dir(Student)))) #求出实例属性
#给实例绑定一个方法,给实例绑定的犯法必须参数加上self
s2 = Student(1,1)
from types import MethodType
s2.str2num1 = MethodType(lambda self, str:reduce(lambda x, y:10 * x + y,map(lambda s:Digital[s],str)), s2)
print(s2.str2num1("8989898"))
#给类在外部绑定一个方法
Student.str2num = lambda self, str:reduce(lambda x, y: 10 *x + y,map(lambda char :Digital[char], str))
#Student.str2num2 = str2num
print(s1.str2num("345"))
#turn methodical function into getting property transferation using @property and @functionname.setter
s1.score = 100
print(s1.score)
print(Student(0,1)) #cite the function of __str__ when construct object
'''Student is a iterable object due to the __ter__ and __next__ before class constructing'''
print(next(s1))
print(next(s1))
print(next(s1))
print(s1.des)
print(s1()) #object call