python-在子类中调用父类的方法
#-*- coding:utf-8 -*- # # from bs4 import BeautifulSoup # # import requests,time # import random # import os # # with open('doc.html') as rf: # # soup = BeautifulSoup(rf,'html.parser') # # print(soup.prettify()) # # soup = BeautifulSoup("<html>data</html>") # # for i in soup.find_all('a'): # # print(i) # # print(i.get('id')) # # sessiona = requests.Session() # # # headers = {'User-Agent':'Mozilla/5.0 (Windows NT 10.0; Win64; x64; rv:47.0) Gecko/20100101 Firefox/47.0'} # # # captcha_content = sessiona.get('https://www.zhihu.com/captcha.gif?r=%d&type=login'%(time.time()*1000),headers=headers).content # # # xyz = sessiona.get('https://www.zhihu.com/#signin',headers=headers).content_xsrf = BeautifulSoup(sessiona.get('https://www.zhihu.com/#signin',headers=headers).content,'html.parser').find('input',attrs={'name':'_xsrf'}).get('value') # # print(type(sessiona)) # # def yanzheng(): # ret = "" # for i in range(4): # num = random.randint(0,9) # alf = chr(random.choice([(random.randint(65,90)),random.randint(97,122)])) # s = str(random.choice([num,alf])) # ret += s # return ret # # if __name__ == '__main__': # # code = yanzheng() # # print(code) # # # print(ord('a')) # # # l = os.path('__file__') # print(os.path.abspath(__file__)) # print(os.path.dirname(os.path.abspath(__file__))) # import sys # import time # # print(sys.arg) #返回参数list # # sys.stdout.write("#") #print() # for i in range(10): # sys.stdout.write("#") # time.sleep(0.5) # sys.stdout.flush() # import json # dic = {'name':'alex'} # fw = open("hello","w") # wdata = json.dumps(dic) #dumps封装成json数据,单引号、双引号都变双引号,json只认识双引号,不认识单引号 # fw.write(wdata) # print(type(wdata)) # fw.close() # # fr = open("hello","r") # rdata = json.loads(fr.read()) # print(type(rdata)) # fr.close() # import time # print(time.time()) # print(time.localtime(time.time())) # print(time.asctime(time.localtime(time.time()))) # print(time.strftime("%Y-%m-%d %H:%M:%S", time.localtime())) # print(time.strptime("2016:12:12:17:50:36","%Y-%m-%d-%X")) # value = "5+9" # v1,v2 = value.split('+') # v3 = int(v1) # # print(int(v1)) # print(type(v3)) # print(type(v2)) # for item in range(1,100,-10): # # print(item) # print(list("asdf")) #列表转字符串(字符串加数字): # li = [11, 22, 33, "asd", "xyz", "879", "hello"] # s = "" # for item in li: # s = s + str(item) # print(s) #列表转字符串(只有字符串): # li = ["ety", "xyz", "hello", "world"] # s = "".join(li) # print(s) # li=[] # li.append("'aaa', '111', 'bbb'") #追加 # # print(li) # li.extend(["sss", "ddd", "fff"]) #扩展(参数为可迭代对象) # # print(li) # li.append(['zz','xx','cc']) # print(li) # v_pop = li.pop() # print(v_pop) # print(li) # info = {'a':1,'b':2,'c':3} # for k,v in info.items(): # print(k,v) # #根据序列创建字典,并制定统一的值 # v = dict.fromkeys(["k1","k2","k3","k4","k5"],"asdf") # print(v) # # #获取字典value # print(v.get('k6',"get失败,该key不存在")) # # #以key值来删除并获取删除的value # #只返回删除的value # print(v.pop('k6',"pop失败,该key不存在")) # # #随机删除键值对 # #返回随机删除的键值对 # print(v.popitem()) # print("pop()后",v) # # #为key设置默认value # v.setdefault("key","value") # # #更新字典: # v.update(k1="111",k5="555") # print("update()后",v) # fr = open("C:\\Users\\user\\PycharmProjects\\untitled\\venv\\xaa.txt", 'r',encoding='UTF-8') # lines = fr.readlines() # print(lines) # msg='liguohui' # print(map(lambda x:x.upper(),msg)) # fr = open('text','r',encoding='utf-8') # lines = fr.readlines() # for line in lines: # data = line.split('\t') # print(data[0]) # # fr.close() # import time # try: # fr = open('text', 'r',encoding='utf-8') # data = fr.read() # time.sleep(1) # print(data) # print(type(data)) # finally: # if fr: # fr.close() # n=0 # for i in range(10): # n+=0.1 # print(n) # str1 = 'asdfgh' # for i in str1: # str[str1.index(i)] = 'l' # a = ['锦','瑟','无','端','五','十','弦'] # 定义一个列表 # print(a) # a=list(('锦','瑟','无','端','五','十','弦'))#强制转换 # print(a) # a = ['锦', '瑟', '无', '端', '五', '十', '弦'] # b = ['一', '弦', '一', '柱', '思', '华', '年'] # a.extend(b) #只是对a进行扩展,无返回值(不会产生新的列表) # print(a) # ['锦', '瑟', '无', '端', '五', '十', '弦', '一', '弦', '一', '柱', '思', '华', '年'] # b.extend(b) # print(b) # ['一', '弦', '一', '柱', '思', '华', '年', '一', '弦', '一', '柱', '思', '华', '年'] # a = ['一', '弦', '一', '柱', '思', '华', '年'] # b='abcd' # a.extend(b) # print(a) # ['一', '弦', '一', '柱', '思', '华', '年', 'a', 'b', 'c', 'd'] #import re # print(re.search('(?P<name>[a-z]+)(?P<age>\d+)',"asd123fgh456jkl789").group()) # print(re.split('[ |]','hello iam|matt')) # print(re.split('[ab]','asdabcd')) # print(re.sub('a','A','aaaaaa')) # print(re.subn('a','A','aaaaaa')) # rule = re.compile('\d') #编译规则 # print(rule.findall("12df34gh5h")) # print(re.findall('www\.(baidu|163|sina)\.com','d3d3d3f45g5gwww.baidu.com2ftg5hth6he')) # print(re.findall('www\.(?:baidu|163|sina)\.com','d3d3d3f45g5gwww.baidu.com2ftg5hth6he')) # print(re.findall('(123|f)','123asd')) # import logging # # logging.basicConfig( # # level=logging.DEBUG, # # filename='catalina.out', # # filemode='a', # # format="%(asctime)s [%(line)d'] %(message)s", # # ) # # logger=logging.getLogger() # # fh=logging.FileHandler('text.log') # ch=logging.StreamHandler() # # fm=logging.Formatter("%(asctime)s %(message)s") # fh.setFormatter(fm) # ch.setFormatter(fm) # # logger.addHandler(fh) # logger.addHandler(ch) # logger.setLevel("ERROR") # # logger.debug("a") # logger.info("b") # logger.warning("c") # logger.error("d") # logger.critical("e") # import re # print(re.findall('(abc)+','abcabcabc')) # print(re.findall('(?:abc)+','abcabcabc')) # print(re.findall('abc+','abcabcabc')) # import configparser # config = configparser.ConfigParser() #config={} # config["DEFAULT"]={'serverliveinternal':45, # 'compressionlevel':9, # 'conpression':'no'} # config["bitbucket"]={'rank':39, # 'security':9, # 'webroot':'yes'} # #配置内容写入配置文件 # with open('config.txt','w') as wf: # config.write(wf) # # #读查文件 # config.read('config.txt') # print(config.sections()) # print(config['bitbucket']['rank']) # print(config.options('bitbucket')) #将keys放入列表 # print(config.items('bitbucket')) #将keys values 放入列表 # print(config.get('bitbucket','webroot')) # # config.add_section('conf2') # config.set('conf2','timeout','300') # config.write(open('config.txt','a')) # import hashlib # obj = hashlib.md5() # obj.update('hello'.encode("utf8")) #encode():字符串转换字节 # print(obj.hexdigest()) # class people: # leixing='buru' # def __init__(self,name,age): # self.name=name # self.age=age # # p1=people('zhangsan','13') # p1.leixing = '2222' # print(p1.leixing) # p2=people('zhangsan1','13') # print(p2.leixing) # print(people.leixing) # people.leixing = '111111111' # print(people.leixing) # print(p1.leixing) # print(p2.leixing) # import time, threading # # # 新线程执行的代码: # def Loop(): # print('thread %s is running...' % threading.current_thread().name) # n = 0 # while n < 5: # n = n + 1 # print('thread %s >>> %s' % (threading.current_thread().name, n)) # time.sleep(1) # print('thread %s ended.' % threading.current_thread().name) # # print('thread %s is running...' % threading.current_thread().name) # t = threading.Thread(target=Loop, name='循环') # t.start() # t.join() # print('thread %s ended.' % threading.current_thread().name) # import time, threading # # # 假定这是你的银行存款: # res = 0 # def change_it(n): # # 先存后取,结果应该为0: # global res # res = res + n # # time.sleep(0.01) # res = res - n # # def run_thread(n): # for i in range(1000000): # change_it(n) # # t1 = threading.Thread(target=run_thread, args=(0,)) # t2 = threading.Thread(target=run_thread, args=(8000,)) # t1.start() # t2.start() # t1.join() # t2.join() # print(res) # import threading,time # class Boss(threading.Thread): # # def run(self): # print("BOSS:今晚大家都要加班到22:00。") # print(event.isSet())# False # event.set() # time.sleep(3) # print("BOSS:<22:00>可以下班了。") # print(event.isSet()) # event.set() # # class Worker(threading.Thread): # def run(self): # # event.wait()# 一旦event被设定,等同于pass # print("Worker:哎……命苦啊!") # time.sleep(1) # event.clear() # event.wait() # print("Worker:OhYeah!") # # # if __name__=="__main__": # event=threading.Event() # # # threads=[] # for i in range(5): # threads.append(Worker()) # threads.append(Boss()) # for t in threads: # t.start() # for t in threads: # t.join() # # print("ending.....") # import queue #线程队列 # # q=queue.Queue() #默认FIFO q=queue.Queue(5) 只能放5个元素 # q.put(12) # q.put('hello') # q.put({'name':'lee'}) # # while 1: # data=q.get() # print(data) # print('------') # import queue # q=queue.LifoQueue() #后进先出模式 # q.put(12) # q.put('hello') # q.put({'name':'lee'}) # # while 1: # data=q.get() # print(data) # print('------') # import queue # q=queue.PriorityQueue() #优先级模式 # q.put([3,12]) # q.put([2,'hello']) # q.put([4,{'name':'lee'}]) # # print(q.qsize()) # print(q.full()) # print(q.empty()) # while 1: # data=q.get(block=False) # print(data) # print('------') # import time,random # import queue,threading # # q = queue.Queue() # # def Producer(name): # count = 0 # while count <10: # print("making........") # time.sleep(5) # q.put(count) # print('Producer %s has produced %s baozi..' %(name, count)) # count +=1 # #q.task_done() # q.join() # print("ok......") # # def Consumer(name): # count = 0 # while count <10: # time.sleep(random.randrange(4)) # # if not q.empty(): # # print("waiting.....") # #q.join() # data = q.get() # print("eating....") # time.sleep(4) # # q.task_done() # #print(data) # print('\033[32;1mConsumer %s has eat %s baozi...\033[0m' %(name, data)) # # else: # # print("-----no baozi anymore----") # count +=1 # # p1 = threading.Thread(target=Producer, args=('A君',)) # c1 = threading.Thread(target=Consumer, args=('B君',)) # c2 = threading.Thread(target=Consumer, args=('C君',)) # c3 = threading.Thread(target=Consumer, args=('D君',)) # # p1.start() # c1.start() # c2.start() # c3.start() # tuple=('1',2,3,3,4,5,6,'a','a') # print(tuple) # def count(n): # print('start...') # while n > 0: # print('before yield') # yield n # print('after yield') # print(n) # n -=1 # # # c=count(10) # next(c) # print('\n') # next(c) # print('\n') # next(c) # a=123 # b=a # print(id(a)) # print(id(b)) # b=124 # print(id(b)) # a=[1,2,3,[3,4,5]] # b=a # print(id(a),id(b)) # b[3][2]=55 # b[2]=33 # print(a,b) # print(id(a),id(b)) # a='asdfg' # b=a # print(id(a),id(b)) # b='qwert' # print(a,b) # print(id(a),id(b)) # a=-1 # b=-1 # print(a is b) # import itchat # itchat.auto_login(hotReload=True) # itchat.send('hello world',toUserName='dy94941') # import re # ret=re.match('a','asf') # print(ret) # print(ret.group()) # ---*--- coding:utf8 ---*--- ''' a = 1 while a < 11: if a == 7: a = a + 1 else: print(a) a = a + 1 a = 1 b = 0 while a <= 101: b = b + a a = a + 1 print(b) a = 1 print("偶数如下:") while a<= 100: if a % 2 == 0: print(a) a = a + 1 else: a = a + 1 pass a = 1 b = 0 while a <= 99: if a % 2 == 0: b = b - a a = a + 1 else: b = b + a a = a + 1 print(b) for counter in range(1,4): print("第 %d 次验证:" % counter) name = input('input ur name') passwd = input('input ur passwd') if name == "1" and passwd == "1": print("succeed") print("welcom %s" % name) break else: print("failed") counter = 1 print("第 %d 次验证:" %counter) name = input('input ur name') passwd = input('input ur passwd') while (name != "root" or passwd != "123456") and counter < 3: counter = counter + 1 print("第 %d 次验证:" % counter) name = input('input ur name') passwd = input('input ur passwd') else: if (name != "root" or passwd != "123456") and counter >= 3: print("验证次数大于3次") else: print("welcom %s" %name) ''' # info = "" # while True: # v1 = input(">>>") # if v1 == "q" or "Q": # break # v2 = input(">>>") # if v2 == "q" or "Q": # break # v3 = input(">>>") # if v3 == "q" or "Q": # break # template = "用户名:{0},\t密码:{1},\t邮箱:{2}\n" # info = info + template.format(v1,v2,v3) # print(info.expandtabs(20)) # int # msg='liguohui' # print(list(map(lambda x:x.upper(),msg))) # 对象关联 # class School: # def __init__(self,name,addr): # self.name=name # self.addr=addr # self.course_list=[] # def zhao_sheng(self): # print('%s 正在招生' %self.name) # class Course: # def __init__(self,name,price,period): # self.name=name # self.price=price # self.period=period # # s1=School('oldboy','北京') # s2=School('oldboy','南京') # s3=School('oldboy','东京') # # c1=Course('linux',10,'1h') # c2=Course('python',10,'1h') # # s1.course_list.append(c1) # s1.course_list.append(c2) # print(s1.__dict__) # # for course_obj in s1.course_list: # print(course_obj.name,course_obj.price) # 在子类中调用父类的方法 class Vehicle: Country='China' def __init__(self,name,speed,load,power): self.name=name self.speed=speed self.load=load self.power=power def run(self): print('开动啦') print('开动啦') class Subway(Vehicle): def __init__(self,name,speed,load,power,line): Vehicle.__init__(self,name,speed,load,power) self.line=line def show_info(self): print(self.name,self.speed,self.load,self.power,self.line) def run(self): Vehicle.run(self) print('%s %s 线,开动啦' %(self.name,self.line)) line13=Subway('北京地铁','10km/s',300,'电',13) line13.show_info() line13.run()