Problem-1

op的功能:对若干个整数进行求和,对若干个字符串进行拼接。
op的执行分为两步。
第一步:调用op函数,传入的参数是操作的名称和操作的参数的类型。返回一个操作实例。这一步只是获取_op的实例化对象,并不执行_op函数内部的逻辑代码。当然,也不会调用_type_checker函数,因为只有执行_op函数的时候才会调用并执行_type_checker函数。
第二步:对第一步调用op函数返回的_op实例化对象作为一个新的函数进行调用,调用的时候只传递实例化参数。然后就会依次执行_op函数。_op函数首先调用_type_checker函数对传入_op函数的参数的个数和类型进行校验,校验的参照物就是调用op函数传递进来的参数。其实是对传递进op函数的arg_types进行校验,首先对参数的个数进行校验,然后对各个参数的类型进行校验。

输出

3
operation add’s 1th argument is of type int, but str given
operation add takes 2 arguments, but 3 given
helloworld
operation concat’s 0th argument is of type str, but int given

Problem-2

def flatten(nested):if isinstance(nested, list):for sublist in nested:for element in flatten(sublist):yield elementelse :yield nestedprint(list(flatten([[[1],2],3,4,[5,[6,7]],8])))

flatten函数的功能:讲嵌套列表中的元素提取出来放在同一个列表当中。
flatten函数的工作机制:flatten函数内部含有yield关键字,所以flatten就变成了生成器函数。使用递归的方式,每碰到一个嵌套的列表,对于该列表中的每一个元素,都会调用flatten函数,因此是递归调用。当然,flatten的递归终止条件是传入flatten的参数不是列表。也就是说,传入flatten的参数是一个数字。但是yield很特殊,第二次调用yield时,它可以从上次调用yield之后的位置处继续执行。所以,当递归调用生成器函数时,实际上只会对同一个位置的元素执行一次yield操作,然后yield就变化了位置。

只有到达递归的最底层之后,会进行递归结束时的退栈操作,这时才会执行yield操作。经过多层的flatten之后,到达最底层的那个生成器函数。所以需要首先从最高层的flatten生成器函数开始进行yield,直到最底层的flatten生成器函数执行yield之后,才算是yield操作完成。也就是说,可以把yield本身可以把它看成是一个递归的函数。因为高层的生成器函数的yield调用了下一层的生成器函数的yield,直到最底层的生成器函数调用yield之后,才算是完成。
综上,yield函数的输出是:[1,2,3,4,5,6,7,8]

Problem-3

decorator函数相当于时一个装饰器,这个和Java中的面向切面变成有些相似。即在执行A操作的时候来执行这个操作的前置操作,或者在A操作执行完成之后,来利用decorator函数来做一些收尾的工作。因为python是一种边解释边执行的变成语言,在执行say_whee()之前会解释执行print(“Something is happening before the function is called.”),在say_wheel()之后会解释执行print(“Something is happening after the function is called.”)。
所以,最后的输出结果时:
print(“Something is happening before the function is called.”)
Whee!
print(“Something is happening after the function is called.”)

Problem-4

a = ‘hello’
s = help(type(a))
print(s)

class str(object)|  str(object='') -> str|  str(bytes_or_buffer[, encoding[, errors]]) -> str|  |  Create a new string object from the given object. If encoding or|  errors is specified, then the object must expose a data buffer|  that will be decoded using the given encoding and error handler.|  Otherwise, returns the result of object.__str__() (if defined)|  or repr(object).|  encoding defaults to sys.getdefaultencoding().|  errors defaults to 'strict'.|  |  Methods defined here:|  |  __add__(self, value, /)|      Return self+value.|  |  __contains__(self, key, /)|      Return key in self.|  |  __eq__(self, value, /)|      Return self==value.|  |  __format__(...)|      S.__format__(format_spec) -> str|      |      Return a formatted version of S as described by format_spec.|  |  __ge__(self, value, /)|      Return self>=value.|  |  __getattribute__(self, name, /)|      Return getattr(self, name).|  |  __getitem__(self, key, /)|      Return self[key].|  |  __getnewargs__(...)|  |  __gt__(self, value, /)|      Return self>value.|  |  __hash__(self, /)|      Return hash(self).|  |  __iter__(self, /)|      Implement iter(self).|  |  __le__(self, value, /)|      Return self<=value.|  |  __len__(self, /)|      Return len(self).|  |  __lt__(self, value, /)|      Return self<value.|  |  __mod__(self, value, /)|      Return self%value.|  |  __mul__(self, value, /)|      Return self*value.n|  |  __ne__(self, value, /)|      Return self!=value.|  |  __new__(*args, **kwargs) from builtins.type|      Create and return a new object.  See help(type) for accurate signature.|  |  __repr__(self, /)|      Return repr(self).|  |  __rmod__(self, value, /)|      Return value%self.|  |  __rmul__(self, value, /)|      Return self*value.|  |  __sizeof__(...)|      S.__sizeof__() -> size of S in memory, in bytes|  |  __str__(self, /)|      Return str(self).|  |  capitalize(...)|      S.capitalize() -> str|      |      Return a capitalized version of S, i.e. make the first character|      have upper case and the rest lower case.|  |  casefold(...)|      S.casefold() -> str|      |      Return a version of S suitable for caseless comparisons.|  |  center(...)|      S.center(width[, fillchar]) -> str|      |      Return S centered in a string of length width. Padding is|      done using the specified fill character (default is a space)|  |  count(...)|      S.count(sub[, start[, end]]) -> int|      |      Return the number of non-overlapping occurrences of substring sub in|      string S[start:end].  Optional arguments start and end are|      interpreted as in slice notation.|  |  encode(...)|      S.encode(encoding='utf-8', errors='strict') -> bytes|      |      Encode S using the codec registered for encoding. Default encoding|      is 'utf-8'. errors may be given to set a different error|      handling scheme. Default is 'strict' meaning that encoding errors raise|      a UnicodeEncodeError. Other possible values are 'ignore', 'replace' and|      'xmlcharrefreplace' as well as any other name registered with|      codecs.register_error that can handle UnicodeEncodeErrors.|  |  endswith(...)|      S.endswith(suffix[, start[, end]]) -> bool|      |      Return True if S ends with the specified suffix, False otherwise.|      With optional start, test S beginning at that position.|      With optional end, stop comparing S at that position.|      suffix can also be a tuple of strings to try.|  |  expandtabs(...)|      S.expandtabs(tabsize=8) -> str|      |      Return a copy of S where all tab characters are expanded using spaces.|      If tabsize is not given, a tab size of 8 characters is assumed.|  |  find(...)|      S.find(sub[, start[, end]]) -> int|      |      Return the lowest index in S where substring sub is found,|      such that sub is contained within S[start:end].  Optional|      arguments start and end are interpreted as in slice notation.|      |      Return -1 on failure.|  |  format(...)|      S.format(*args, **kwargs) -> str|      |      Return a formatted version of S, using substitutions from args and kwargs.|      The substitutions are identified by braces ('{' and '}').|  |  format_map(...)|      S.format_map(mapping) -> str|      |      Return a formatted version of S, using substitutions from mapping.|      The substitutions are identified by braces ('{' and '}').|  |  index(...)|      S.index(sub[, start[, end]]) -> int|      |      Like S.find() but raise ValueError when the substring is not found.|  |  isalnum(...)|      S.isalnum() -> bool|      |      Return True if all characters in S are alphanumeric|      and there is at least one character in S, False otherwise.|  |  isalpha(...)|      S.isalpha() -> bool|      |      Return True if all characters in S are alphabetic|      and there is at least one character in S, False otherwise.|  |  isdecimal(...)|      S.isdecimal() -> bool|      |      Return True if there are only decimal characters in S,|      False otherwise.|  |  isdigit(...)|      S.isdigit() -> bool|      |      Return True if all characters in S are digits|      and there is at least one character in S, False otherwise.|  |  isidentifier(...)|      S.isidentifier() -> bool|      |      Return True if S is a valid identifier according|      to the language definition.|      |      Use keyword.iskeyword() to test for reserved identifiers|      such as "def" and "class".|  |  islower(...)|      S.islower() -> bool|      |      Return True if all cased characters in S are lowercase and there is|      at least one cased character in S, False otherwise.|  |  isnumeric(...)|      S.isnumeric() -> bool|      |      Return True if there are only numeric characters in S,|      False otherwise.|  |  isprintable(...)|      S.isprintable() -> bool|      |      Return True if all characters in S are considered|      printable in repr() or S is empty, False otherwise.|  |  isspace(...)|      S.isspace() -> bool|      |      Return True if all characters in S are whitespace|      and there is at least one character in S, False otherwise.|  |  istitle(...)|      S.istitle() -> bool|      |      Return True if S is a titlecased string and there is at least one|      character in S, i.e. upper- and titlecase characters may only|      follow uncased characters and lowercase characters only cased ones.|      Return False otherwise.|  |  isupper(...)|      S.isupper() -> bool|      |      Return True if all cased characters in S are uppercase and there is|      at least one cased character in S, False otherwise.|  |  join(...)|      S.join(iterable) -> str|      |      Return a string which is the concatenation of the strings in the|      iterable.  The separator between elements is S.|  |  ljust(...)|      S.ljust(width[, fillchar]) -> str|      |      Return S left-justified in a Unicode string of length width. Padding is|      done using the specified fill character (default is a space).|  |  lower(...)|      S.lower() -> str|      |      Return a copy of the string S converted to lowercase.|  |  lstrip(...)|      S.lstrip([chars]) -> str|      |      Return a copy of the string S with leading whitespace removed.|      If chars is given and not None, remove characters in chars instead.|  |  partition(...)|      S.partition(sep) -> (head, sep, tail)|      |      Search for the separator sep in S, and return the part before it,|      the separator itself, and the part after it.  If the separator is not|      found, return S and two empty strings.|  |  replace(...)|      S.replace(old, new[, count]) -> str|      |      Return a copy of S with all occurrences of substring|      old replaced by new.  If the optional argument count is|      given, only the first count occurrences are replaced.|  |  rfind(...)|      S.rfind(sub[, start[, end]]) -> int|      |      Return the highest index in S where substring sub is found,|      such that sub is contained within S[start:end].  Optional|      arguments start and end are interpreted as in slice notation.|      |      Return -1 on failure.|  |  rindex(...)|      S.rindex(sub[, start[, end]]) -> int|      |      Like S.rfind() but raise ValueError when the substring is not found.|  |  rjust(...)|      S.rjust(width[, fillchar]) -> str|      |      Return S right-justified in a string of length width. Padding is|      done using the specified fill character (default is a space).|  |  rpartition(...)|      S.rpartition(sep) -> (head, sep, tail)|      |      Search for the separator sep in S, starting at the end of S, and return|      the part before it, the separator itself, and the part after it.  If the|      separator is not found, return two empty strings and S.|  |  rsplit(...)|      S.rsplit(sep=None, maxsplit=-1) -> list of strings|      |      Return a list of the words in S, using sep as the|      delimiter string, starting at the end of the string and|      working to the front.  If maxsplit is given, at most maxsplit|      splits are done. If sep is not specified, any whitespace string|      is a separator.|  |  rstrip(...)|      S.rstrip([chars]) -> str|      |      Return a copy of the string S with trailing whitespace removed.|      If chars is given and not None, remove characters in chars instead.|  |  split(...)|      S.split(sep=None, maxsplit=-1) -> list of strings|      |      Return a list of the words in S, using sep as the|      delimiter string.  If maxsplit is given, at most maxsplit|      splits are done. If sep is not specified or is None, any|      whitespace string is a separator and empty strings are|      removed from the result.|  |  splitlines(...)|      S.splitlines([keepends]) -> list of strings|      |      Return a list of the lines in S, breaking at line boundaries.|      Line breaks are not included in the resulting list unless keepends|      is given and true.|  |  startswith(...)|      S.startswith(prefix[, start[, end]]) -> bool|      |      Return True if S starts with the specified prefix, False otherwise.|      With optional start, test S beginning at that position.|      With optional end, stop comparing S at that position.|      prefix can also be a tuple of strings to try.|  |  strip(...)|      S.strip([chars]) -> str|      |      Return a copy of the string S with leading and trailing|      whitespace removed.|      If chars is given and not None, remove characters in chars instead.|  |  swapcase(...)|      S.swapcase() -> str|      |      Return a copy of S with uppercase characters converted to lowercase|      and vice versa.|  |  title(...)|      S.title() -> str|      |      Return a titlecased version of S, i.e. words start with title case|      characters, all remaining cased characters have lower case.|  |  translate(...)|      S.translate(table) -> str|      |      Return a copy of the string S in which each character has been mapped|      through the given translation table. The table must implement|      lookup/indexing via __getitem__, for instance a dictionary or list,|      mapping Unicode ordinals to Unicode ordinals, strings, or None. If|      this operation raises LookupError, the character is left untouched.|      Characters mapped to None are deleted.|  |  upper(...)|      S.upper() -> str|      |      Return a copy of S converted to uppercase.|  |  zfill(...)|      S.zfill(width) -> str|      |      Pad a numeric string S with zeros on the left, to fill a field|      of the specified width. The string S is never truncated.|  |  ----------------------------------------------------------------------|  Static methods defined here:|  |  maketrans(x, y=None, z=None, /)|      Return a translation table usable for str.translate().|      |      If there is only one argument, it must be a dictionary mapping Unicode|      ordinals (integers) or characters to Unicode ordinals, strings or None.|      Character keys will be then converted to ordinals.|      If there are two arguments, they must be strings of equal length, and|      in the resulting dictionary, each character in x will be mapped to the|      character at the same position in y. If there is a third argument, it|      must be a string, whose characters will be mapped to None in the result.None

Problem-5

“” ➞ 0
“5 6 +” ➞ 11
“6 5 5 7 * - /” ➞ 5
“x y +” ➞ Invalid instruction or operand

class MyException(Exception): # 定义自己的异常类型def __init__(self,msg):self.msg = msgdef __str__(self):return self.msgclass Stack:def __init__(self):self.opes = [] # 存放运算符self.nums = [] # 存放操作数def push_operate(self,o):self.opes.append(o)def pop_operate(self):return self.opes.pop()def push_num(self,o):self.nums.append(o)def pop_num(self):return self.nums.pop()def __del__(self): # 析构函数,用于删除不用的资源del self.opesdel self.numsif __name__ == "__main__":s = Stack()opes = input('please input:')c2 = opes.strip('"')have_exception = 0if c2 == '':print(0)else:try:c = c2.split(' ') # 分离出运算数和运算符,组成列表赋值给cfor i in c: # 对所有的数字进行操作if i not in ['+','-','*','/']:s.push_num(i) # 存放操作数for i in range(0, len(c))[::-1]: # 倒着输出下标if c[i] in ['+','-','*','/']:s.push_operate(c[i]) # 存放操作符if len(s.opes) == 0: # 如果操作符为空raise MyException("Invalid instruction or operand")while len(s.opes): # 依次检查每一个字符try:n1 = int(s.pop_num())n2 = int(s.pop_num())n3 = 0op = s.pop_operate()if op == '+':n3 = n1 + n2# print("{}{}{}={}".format(n1, op, n2, n3))  # 打印出运算过程elif op == '-':n3 = n1 - n2# print("{}{}{}={}".format(n1, op, n2, n3))elif op == '*':n3 = n1 * n2# print("{}{}{}={}".format(n1, op, n2, n3))elif op == '/':n3 = n1 / n2# print("{}{}{}={}".format(n1, op, n2, n3))s.push_num(n3)except Exception as e:raise MyException('Invalid instruction or operand')breakexcept MyException as me:have_exception = 1print(me.msg)if have_exception == 0:print('{}'.format(s.pop_num()))

python五问小练习相关推荐

  1. 【Python五篇慢慢弹】快速上手学python

    快速上手学python 作者:白宁超 2016年10月4日19:59:39 摘要:python语言俨然不算新技术,七八年前甚至更早已有很多人研习,只是没有现在流行罢了.之所以当下如此盛行,我想肯定是多 ...

  2. python小程序源代码-整理了适合新手的20个Python练手小程序

    100个Python练手小程序,学习python的很好的资料,覆盖了python中的每一部分,可以边学习边练习,更容易掌握python. 本文附带基础视频教程:私信回复[基础]就可以获取的 [程序1] ...

  3. python开发微信小程序-微信小程序开发:python+sanic 实现小程序登录注册

    开发微信小程序时,接入小程序的授权登录可以快速实现用户注册登录的步骤,是快速建立用户体系的重要一步.这篇文章将介绍 python + sanic + 微信小程序实现用户快速注册登录全栈方案. 微信小程 ...

  4. python小程序-整理了适合新手的20个Python练手小程序

    即刻关注公众号,发现世界的美好 100个Python练手小程序,学习python的很好的资料,覆盖了python中的每一部分,可以边学习边练习,更容易掌握python. [程序1] 题目:有1.2.3 ...

  5. 太强了!Python 开发桌面小工具,让代码替我们干重复的工作!

    作者:Cherish 来源:https://www.jianshu.com/p/91128d442198 决定写这篇文章的初衷是来源于一位小伙伴的问题,关于"如何根据数据源用 Python ...

  6. 【Python五篇慢慢弹】数据结构看python

    数据结构看python 作者:白宁超 2016年10月9日14:04:47 摘要:继<快速上手学python>一文之后,笔者又将python官方文档认真学习下.官方给出的pythondoc ...

  7. Python 开发桌面小工具,让代码替我们干重复的工作!

    作者:Cherish 来源:https://www.jianshu.com/p/91128d442198 本文为读者投稿 决定写这篇文章的初衷是来源于一位小伙伴的问题,关于"如何根据数据源用 ...

  8. 【Python五篇慢慢弹(5)】类的继承案例解析,python相关知识延伸

    类的继承案例解析,python相关知识延伸 作者:白宁超 2016年10月10日22:36:57 摘要:继<快速上手学python>一文之后,笔者又将python官方文档认真学习下.官方给 ...

  9. python编程100个小程序-【Python精华】100个Python练手小程序

    100个Python练手小程序,学习python的很好的资料,覆盖了python中的每一部分,可以边学习边练习,更容易掌握python. [程序1] 题目:有1.2.3.4个数字,能组成多少个互不相同 ...

最新文章

  1. 【转贴】PLSQL不安装客户端连接远程oracle
  2. Codeforces round 396(Div. 2) 题解
  3. mysql周报内容范文_Mysql各种报表查询含实例,日报,周报,月报,时间差自动计算...
  4. python二级简书_12月4日,总结发现杯,备战python二级
  5. [Typecho主题] modernist
  6. 基于JAVA+SpringMVC+Mybatis+MYSQL的民宿客栈系统
  7. cropbox.js 头像裁剪插件
  8. 解决ajax异步请求数据后swiper不能循环轮播(loop失效)问题、滑动后不能轮播的问题。
  9. 深度学习 + OpenCV,Python实现实时视频目标检测
  10. java后台返回html_SpringMvc返回html页面字符串
  11. nv21转jpg c语言,Yuv420转Jpeg(C语言实现)
  12. 拓端tecdat|sas文本挖掘案例:如何使用SAS计算Word Mover的距离
  13. 马尔可夫随机场与条件随机场
  14. 奇迹mu开服教程:服务端的架设及开服注意事项
  15. 汇编语言基础:寄存器和寻址方式
  16. Qt中系统屏幕键盘打开与关闭
  17. 前端下载excel文件的两种方式
  18. android使用xml定义背景边框
  19. 揪出毛孔粗大4个致命原因
  20. yum安装报错:ImportError: No module named urlgrabber.grabber

热门文章

  1. java小白零基础(九)
  2. python3.7.7版本,成功安装pyHook模块
  3. usb 与pci驱动的关系
  4. PS实例一 制作凹陷的按钮(斜面和浮雕的使用)
  5. 开发者应有的长度,宽度和深度
  6. IDEA断点调试技巧
  7. 在secureCRT下常用的一些shell 命令
  8. 网速dns怎么调快_PS4dns怎么设置网速快
  9. uiautomator 横向截屏异常解决
  10. 大数据信息资料采集:商品促营销活动海报设计模板八爪鱼采集规则