Python basis for the development of [high-level functions and object-oriented]

Python basic exercises [advanced functions and object-oriented]

1. What are the conditions (1) closures need to meet are?
A. defined outside the inner function B. Functions function using a non-global variables outside the function outer C. The final function returns a reference to the function
(2) in accordance with the above three conditions to create a closure for calculating the square root function
program: [typesetting program according python programming specifications, to be noted that a space corresponding to a coding level]

#函数闭包
#outerfunc为外函数
import math
def outerfunc():
    y =int(input())      #y是外函数的非全局变量

    #innerfunc是内函数
    def innerfunc():

        #内函数使用了外函数的非全局变量
        return math.sqrt(y)

    #外函数最终返回的是内函数的引用
    return innerfunc
#然后执行如下代码:
func = outerfunc()
sqrtone=func()
print("内函数计算后的平方根是:"+str(sqrtone))
#输入16输出4
#因为外函数最终返回的是内函数的引用,所以变量func为内函数innerfunc的引用,
#func(),变量后面加了括号,则是执行内函数,所以最终结果为4。
#内函数实现的是返回外函数的引用,外函数实现的是具体的运算功能

The results are shown:
Here Insert Picture Description
2. a conventional function as follows:
Function content
claims the test function is created by decorators, decorator function has parameters into the printing parameters into the ornamental decorative function, do the required results as follows:
Here Insert Picture Description

程序:
#开始创建装饰器
def q(w1):
    def inner(num1,num2):
        print("装饰器开始装饰:")
        print("--打印装饰器的入参为:",num1,num2,"我是装饰器的入参啊")
        w1(num1,num2)
        return  'huatec'
    return inner
@q
def test(num1,num2):
    print("被验证函数开始,被修饰函数入参之和为:",num1+num2)
    return 'huatec'
#调用test函数
print(test(1,2))

The results are shown:
Here Insert Picture Description
3. The filter function used to print out all the range of 1 to 100 3 can be divisible by the number of
programs:

 #filter() 函数用于过滤序列,过滤掉不符合条件的元素,返回由符合条件元素组成的新列表。
# 该函数接收两个参数,第一个为函数,第二个为序列,序列的每个元素作为参数传递给函数进行判,然后返回 True 或 False,最后将返回 True 的元素放到新列表中。
import math
#先定义判断函数,返回判断后的结果值
def bszc(x):
    return x%3==0
#开始调用过滤函数,过滤时需要的参数是判断函数和列表,将列表中的元素一一放到判断函数中判断,过滤后返回满足判断函数的元素所组成的新列表,此时是引用变量
newlist = filter(bszc,range(1, 101))
print(list(newlist))
#print函数在打印变量时一般会以字符串形式显现,如果没有打印出目标变量而是字符串的话,用强制转换来进行(据具体的类型而变换)

The results are shown:
Here Insert Picture Description
4. Enter a number using the factorial reduce this number:
Here Insert Picture Description
Program:


#reduce() 函数会对参数序列中元素进行[累积。]函数将一个数据集合(链表,元组等)中的所有数据进行下列操作:用传给 reduce 中的函数 function(有两个参数)先对集合中的第 1、2 个元素进行操作,得到的结果再与第三个数据用 function 函数运算,最后得到一个结果。
from functools import reduce
def leiji(x,y):
    return x*y
#随意给个数求阶乘,就要生成一个列表,这里创建列表
list  = []
#随意给一个数
i=int(input())
for x in range(1,i+1):
    list.append(x)
    print(list)
#for代表循环取数。
#append()函数代表将值插入到列表最后。
reduce(leiji,list)
print(int(reduce(leiji,list)))

The results are shown:
Here Insert Picture Description
5. Overload achieve multiplication, multiplying corresponding elements of the two lists [multiplying two numbers when the original method, of course, the result of multiplying two lists or a list].
program:

 #方法是定义在类中的,函数是直接定义的,题中要求做到方法的重载,就要定义一个类,定义方法,再方法重载
import numpy as np
class TestChengFa(object):
    def __init__(self,high,old):
        self.high=high
        self.old=old
    def chenfa(self,a,b):
        return a*b
#进行方法的重载,将两个数相乘改为两个列表相乘
    def chenfa(self,listone,listtwo): #形参定义为两个列表
        listthree= np.multiply(np.array(listone),np.array(listtwo))
        print(listthree)
#创建对象
jack = TestChengFa(2,20)
print("开始调用原方法:")
print(jack.chenfa(4,5))
#重载方法的调用
list1 = [1,2,3,4]
list2 = [5,6,7,8]
print("开始调用重载的方法")
jack.chenfa(list1,list2)

As shown:
Here Insert Picture Description
6. Complete fish and waterfowl birds create multiple inheritance. As shown in FIG:
Here Insert Picture Description
Program:

 #定义鱼类
class Fish(object):
    def __init__(self,name):
        self.name=name
        print("这是鱼的构造方法,它的名字是:"+self.name)
    def aoyou(self):
        print("鱼的特点是会遨游")
#定义鸟类
class Bird(object):
    def __init__(self,name):
        self.name=name
        print("这是鸟的构造方法,它的名字是:"+self.name)
    def feixiang(self):
        print("鸟的特点是会飞翔")
#定义水鸟类来多继承鱼儿和鸟儿的特点,此时的水鸟就是鱼儿和鸟儿的派生类
class WaterBird(Fish,Bird):
    def __init__(self,name):
        self.name=name
        print("这是水鸟的构造方法,它的名字是:"+self.name)
#开始创建鸟类,鱼类,水鸟类的对象,比较他们之间的异同来体会多继承
fishone=Fish("小黄鱼")
fishone.aoyou()
birdone=Bird("小鸟")
birdone.feixiang()
waterbirdone=WaterBird("小水鸟")
#水鸟机继承了鸟和鱼,所以能调用他们的方法
waterbirdone.feixiang()
waterbirdone.aoyou()

The results shown in FIG:
Here Insert Picture Description
7. a prior python classes are as follows:
Here Insert Picture Description
Create an instance, according to "instance attributes." Manner respectively print out the class and instance properties, the print attribute requirements of class 9, 5 is an instance attribute, according to "category. properties "manner, to print out all the attributes that can be printed, wherein the printing requirements of class attribute value of 6, the printing section results as shown below:
Here Insert Picture Description
procedure:

 class Cat(object):
#类属性,位于方法外面,类里面,被类的所有实例对象共有
    number=0
    def __init__(self,age):
        #self.属性的属性都是实例属性
        self.age=age
        print("这是猫的构造方法,年龄是:"+self.age+" 它的实例属性是age,类属性是number")
#开始按要求打印类属性和实例属性
catone=Cat("5")
#开始用实例打印类属性
print("cat的类属性是:"+str(catone.number))#默认是0
#打印出9
Cat.number=9
print("cat的类属性是:"+str(catone.number))
#体验下用另一个对象调用类属性,因为被共用,所以打印出的值还是9
cattwo=Cat("5")
print("cat的类属性还是:"+str(cattwo.number))
#开始用实例打印实例属性
print("cat的实例属性是:"+str(catone.age))
#按照类.属性的方式打印所有属性,其实只能打印类属性
Cat.number=6
print("Cat的类属性是:"+str(Cat.number))

结果如图:
Here Insert Picture Description
8.创建一个类,要求内部包含一个用于求和的类方法,一个用于打印求和结果的静态方法,如图所示:
Here Insert Picture Description
程序:

class TestOne(object):
    #定义求和的类方法
    @classmethod
    def qiuhe(cls,a,b):
        return a+b
    #定义用于打印求和结果的类方法,静态方法和类没有直接关系,起到类似函数的作用
    @staticmethod
    def dayin(a,b):
        print("求和后的结果是:"+str(TestOne.qiuhe(a,b)))
#开始调用类方法
TestOne.qiuhe(3,4)
#开始调用静态打印方法
testone=TestOne()
testone.dayin(3,4)
 

结果如图:
Here Insert Picture Description
9.参照“模板”章节课件,完成模块的制作,发布和安装
(1)模块的制作
Here Insert Picture Description
#按照上图目录结构,创建名为suba.subb的包,在包里分别创建aa.py,bb.py,cc.py,dd.py的模块文件,每个模块都完成一定的功能(即定义一个函数),创建过程省略,结果如图:
Here Insert Picture Description
#再创建名为setup的.py文件,与suba.subb在同一目录下,并编辑文件,指明要包含aa.py,bb.py,cc.py,dd.py模块文件以及即将发布的模块包的版本,作者和描述信息,如图:
Here Insert Picture Description
Here Insert Picture Description
(2)模块的发布
#【在终端进入pycharm的默认工作台目录:C:\Users\任重文\PycharmProjects\untitled,该目录下存放了所有的工程文件】
Here Insert Picture Description
Here Insert Picture Description
#开始构建模块【在终端执行python setup.py build命令】

Here Insert Picture Description
#构建完成后工作台文件夹出现了build-lib目录,该目录下有suba.subb两个模块包,如图:
Here Insert Picture Description
Here Insert Picture Description
#构建后的目录结构
Here Insert Picture Description
#生成压缩发布包【即模块的打包】
Here Insert Picture Description
#打包后的目录结构
Here Insert Picture Description
(3)模块的安装
#找到模块的压缩包,对压缩包进行解压
Here Insert Picture Description
#解压后的文件结构
Here Insert Picture Description
#安装模块【在终端执行命令python setup.py install】
Here Insert Picture Description
#安装后的模块目录是D\python\lib\site-packages,pycharm上打开后的图片
Here Insert Picture Description

实验总结与分析:

Question 1: did the fifth question, there have been problems as shown below:
Here Insert Picture Description
Solution:
# Python software for a new installation, in addition to basic library, there is no other function in the library when we need to import It will prompt No module named "XXX". How to address the following issues?
1. Determine if he did not relevant module
cmd- into the Python Shell- input from XXX (library name) import *, if No module named "XXX" appears to prove there is no security
Here Insert Picture Description
2. The module is not found, a module download [Download module: UCI module library address: https://www.lfd.uci.edu/~gohlke/pythonlibs/]
# interface address:
Here Insert Picture Description
3.ctrl + F to find a suitable version according to our use of library name search for the desired file (ie NumPy) FIG:
Here Insert Picture Description
4 is mounted just downloaded files: CMD- input pip (Python) + version number of the install repository path + file name
[numpy] pip3.7 install D: \ Python \ Scripts \ numpy-1.15.0rc1 + mkl cp37m-win_amd64.whl--cp37
# do have a problem in this way, too complex
5. In other second method, the command line executed directly: PIP3 install numpy
Here Insert Picture Description
# prompt after successful installation
Here Insert Picture Description
# here on the successful resolution of the
# way what, when doing these exercises some of his shots is not clear, we make do and see. But the program has to write detailed notes of his own thinking and knowledge of online search, hoping to be useful.

Guess you like

Origin blog.csdn.net/weixin_43306493/article/details/90601128