先碎碎念一下自己的一番不易操作,那个语音播放的东西我总是搞不起来,就很难受,然后发现pycharm倒是可以直接弄,强推一下这个Pycharm中安装并导入pywin32包遇到的问题及解决-CSDN博客
- import win32com.client
- #1.创建一个播报器对象
- speaker = win32com.client.Dispatch('SAPI.SpVoice')
- #2.通过这个播报器对象,直接,播放相对应的语音字符串就可以
- speaker.Speak('我的名字是yx')
- class Caculator:
- def __say(self,word):
- # 1.创建一个播报器对象
- speaker = win32com.client.Dispatch('SAPI.SpVoice')
- # 2.通过这个播报器对象,直接,播放相对应的语音字符串就可以
- speaker.Speak(word)
-
- def __checknumber_zsq(func):
- def inner(self,n):
- if not isinstance(n,int):
- raise TypeError('当前这个数据的类型有问题,应该是一个征信数据')
- return func(self,n)
- return inner
- @__checknumber_zsq
- def __init__(self,num):
- self.__say(num)
- self.__result = num
- @__checknumber_zsq
- def jia(self,n):
- self.__say(n)
- self.__result += n
- @__checknumber_zsq
- def jian(self,n):
- self.__say(n)
- self.__result -=n
- @__checknumber_zsq
- def cheng(self,n):
- self.__say(n)
- self.__result *=n
- @__checknumber_zsq
- def chu(self,n):
- self.__say(n)
- self.__result /=n
- def show(self):
- print('计算的结果是:%d'% self.__result)
- speaker = win32com.client.Dispatch('SAPI.SpVoice')
- speaker.Speak('计算的结果是:%d'% self.__result)
- c1 = Caculator(2)
- c1.jia(6)
- c1.show()
这里checknumber就成了一个装饰器,至于为什么是装饰器,传入的参数怎么样,就是之前那个很烦很复杂的一个分析流程过程。
然后,应该有一种感觉就是,不仅仅是就是函数的写入,应该把他封装成为一个装饰器。
碎碎念一下 女孩子真的好容易焦虑,好容易好容易啊,好容易犯错呀。
1.比如说为什么会报错name '_Caculator__checknumber' is not defined?应该把装饰器写在类里面
2.object() takes no parameters应该将定义def jia的这个函数与装饰器同样的缩进
3.'NoneType' object is not callable就是千万千万不要忘了去写返回值return inner
- import win32com.client
- class Caculator:
- def __say_zsq0(func):
- def inner(self,n):
- speaker = win32com.client.Dispatch('SAPI.SpVoice')
- speaker.Speak(n)
- return func(self,n)
- return inner
- def __say_zsq1(func):
- def inner(self,n):
- speaker = win32com.client.Dispatch('SAPI.SpVoice')
- speaker.Speak('加%d'% n)
- return func(self,n)
- return inner
- def __say_zsq2(func):
- def inner(self,n):
- speaker = win32com.client.Dispatch('SAPI.SpVoice')
- speaker.Speak('减%d'% n)
- return func(self,n)
- return inner
-
- def __say_zsq3(func):
- def inner(self, n):
- speaker = win32com.client.Dispatch('SAPI.SpVoice')
- speaker.Speak('乘%d' % n)
- return func(self, n)
-
- return inner
- def __say_zsq4(func):
- def inner(self, n):
- speaker = win32com.client.Dispatch('SAPI.SpVoice')
- speaker.Speak('除%d' % n)
- return func(self, n)
-
- return inner
- def __checknumber(func):
- def inner(self,n):
- if not isinstance(n,int):
- raise TypeError('输入的不是整型,请重新输入')
- return func(self,n)
- return inner
- @__checknumber
- @__say_zsq0
- def __init__(self,num):
- self.__result = num
- @__checknumber
- @__say_zsq1
- def jia(self,n):
- self.__result +=n
- @__checknumber
- @__say_zsq2
- def jian(self,n):
- self.__result -=n
-
- @__checknumber
- @__say_zsq3
- def cheng(self,n):
- self.__result *= n
-
- @__checknumber
- @__say_zsq4
- def chu(self,n):
- self.__result /= n
- def show(self):
- print('最后算的的结果是%d'%self.__result)
- c1 = Caculator(2)
- c1.jia(3)
- c1.jian(1)
- c1.cheng(5)
- c1.show()
但是这个代码还是很冗余,现在要解决代码冗余的问题
- import win32com.client
- class Caculator:
- def creat_say_zsq(word=''):
- def __say_zsq(func):
- def inner(self,n):
- speaker = win32com.client.Dispatch('SAPI.SpVoice')
- speaker.Speak(word + str(n))
- return func(self,n)
- return inner
- return __say_zsq
- def __checknumber(func):
- def inner(self,n):
- if not isinstance(n,int):
- raise TypeError('输入的不是整型,请重新输入')
- return func(self,n)
- return inner
- @__checknumber
- @creat_say_zsq()
- def __init__(self,num):
- self.__result = num
- @__checknumber
- @creat_say_zsq('加')
- def jia(self,n):
- self.__result +=n
- @__checknumber
- @creat_say_zsq('减')
- def jian(self,n):
- self.__result -=n
-
- @__checknumber
- @creat_say_zsq('乘')
- def cheng(self,n):
- self.__result *= n
-
- @__checknumber
- @creat_say_zsq('除')
- def chu(self,n):
- self.__result /= n
- def show(self):
- print('最后算的的结果是%d'%self.__result)
- c1 = Caculator(2)
- c1.jia(3)
- c1.jian(1)
- c1.cheng(5)
- c1.show()
这是一个很有意思的地方在于,装饰器里面写了两个小闭包!!
- def creat_say_zsq(word=''):
- def __say_zsq(func):
- def inner(self,n):
- speaker = win32com.client.Dispatch('SAPI.SpVoice')
- speaker.Speak(word + str(n))
- return func(self,n)
- return inner
- return __say_zsq