wuqramy 发表于 2020-3-12 17:39:05

Python学习心情记录 2020/3/12

本帖最后由 wuqramy 于 2020-3-12 17:56 编辑

刷题时间到!{:10_312:}
No.1:
0. 定义一个类,当实例化该类的时候,自动判断传入了多少个参数,并显示出来:
>>> c = C()
并没有传入参数
>>> c = C(1, 2, 3)
传入了 3 个参数,分别是:1 2 3
EASY~
class Duixiang():
    def __init__(self,*string):
      if not string:
            print('并没有传入参数')
      else:
            print('传入了',len(string),'个参数,分别是:',end = '')
            for each in string:
                print(each,end = '')
Duixiang()
Duixiang(1,2,3)

成功了!
{:10_297:} {:10_298:} 快乐来得太突然{:10_298:} {:10_297:}
No.2:
1. 定义一个单词(Word)类继承自字符串,重写比较操作符(参考自学:Python 魔法方法详解),当两个 Word 类对象进行比较时,根据单词的长度来进行比较大小。
代码好像太长了...........
class Word(str):
    def __lt__(self, other):
      if len(self) < len(other):
            return True
      else:
            return False
    def __le__(self, other):
      if len(self) <= len(other):
            return True
      else:
            return False
    def __eq__(self, other):
      if len(self) == len(other):
            return True
      else:
            return False
    def __ne__(self, other):
      if len(self) != len(other):
            return True
      else:
            return False
    def __gt__(self, other):
      if len(self) > len(other):
            return True
      else:
            return False
    def __ge__(self, other):
      if len(self) >= len(other):
            return True
      else:
            return False
a = Word('short')
b = Word('long')
print(a < b) #False
print(a <= b) #False
print(a == b) #False
print(a != b) #True
print(a > b) #True
print(a >= b) #True

哦,成功了!
{:10_297:} {:10_298:} 快乐来得太突然{:10_298:} {:10_297:}
轻松的一天~

乘号 发表于 2020-3-12 17:52:22

今天才12号,你的标题怎么是13号的

wuqramy 发表于 2020-3-12 17:56:23

乘号 发表于 2020-3-12 17:52
今天才12号,你的标题怎么是13号的

改了,好吧
页: [1]
查看完整版本: Python学习心情记录 2020/3/12