python补充2

一 关于类中的self以及继承问题

请看下面一段代码

class Base:
    def f1(self):
        self.f2()
 
    def f2(self):
        print('...')
 
class Foo(Base):
    def f2(self):
        print('9999')
 
obj = Foo()
obj.f1() #结果为9999

解说:Foo().f1() -->里面的self指的是实例化的对象,而对象继承的是Foo

实例化类Foo,执行对象的f1方法。由于对象没有f1方法,从父类Base中寻找。
找到f1方法,def f1(self): 此时self指向的是Foo类的对象。执行self.f2()
由于self是Foo类中的,从Foo类中寻找f2方法。找到了,执行输出9999
 

二类的私有属性继承问题

class JustCounter:
    __secretCount = 0

    def count(self):
        self.__secretCount += 1
        print(self.__secretCount)


class Bars(JustCounter):

    def count(self):
        print(self.__secretCount)


counter1 = JustCounter()
counter2 = Bars()

counter1.count()
counter2.count() #类的私有属性不会被继承,这里会报错
View Code

 三递归函数中变量累加覆盖的问题

使用递归函数的时候,需要一个变量一直累加,但是一旦重新递归函数的时候这个变量也会改变,怎么去做

import os
def get_size(path):
    ret=os.listdir(path)
    # print(ret)
    total=0
    for name in ret:
        # print(name)
        abs_path=os.path.join(path,name)
        if os.path.isdir(abs_path):
            total+=get_size(abs_path) #这里递归的同时total也累加了
        else:
            total+=os.path.getsize(abs_path)
    return total #这里总的大小会稍有变化
print(get_size(r'G:studyday13'))

 四计算目录的大小

上面的例子是一个

下面使用堆栈的概念

import os
def get_size(path):
    dir_lst=[path]
    total=0
    while dir_lst:
        path=dir_lst.pop()
        ret=os.listdir(path)
        for name in ret:
            abs_path=os.path.join(path,name)
            if os.path.isdir(abs_path):
                dir_lst.append(abs_path)
            else:
                total+=os.path.getsize(abs_path)
    return total
print(get_size(r'G:studyday13'))
View Code

 找出目录下面的所有文件,并按照从大到小排列

import os
my_list=[]
def get_size(path):
    dir_lst=[path]
    while dir_lst:
        path=dir_lst.pop()
        ret=os.listdir(path)
        for name in ret:
            abs_path=os.path.join(path,name)
            if os.path.isdir(abs_path):
                dir_lst.append(abs_path)
            else:
                my_list.append((abs_path,os.path.getsize(abs_path)))
    return my_list
aa=get_size(r'G:studyday22study-moreversion')
# print(aa)

bb=sorted(aa,key=lambda x:x[1],reverse=True)
print(bb)

五 递归中return的真正理解,跳出函数是跳出最近的函数,以三级菜单举例

menu = {
    '北京': {
        '海淀': {
            '五道口': {
                'soho': {},
                '网易': {},
                'google': {}
            },
            '中关村': {
                '爱奇艺': {},
                '汽车之家': {},
                'youku': {},
            },
            '上地': {
                '百度': {},
            },
        },
        '昌平': {
            '沙河': {
                '老男孩': {},
                '北航': {},
            },
            '天通苑': {},
            '回龙观': {},
        },
        '朝阳': {},
        '东城': {},
    },
    '上海': {
        '闵行': {
            "人民广场": {
                '炸鸡店': {}
            }
        },
        '闸北': {
            '火车战': {
                '携程': {}
            }
        },
        '浦东': {},
    },
    '山东': {},
}
菜单字典
def menu3(menu):
    while True:
        for key in menu:
            print(key)
        inp=input('>>>').strip()
        if inp in menu:
            ret=menu3(menu[inp]) #递归调用menu3函数,将是一个嵌套
            if ret=='q':return 'q'
        elif inp=='b':
            return 'b' #单纯的返回
        elif inp=='q':return 'q' #进入程序单独输入q,直接就是退出menu3函数,这个是没有嵌套的
        #进入菜单一直往下走的话,就是使用q在往嵌套函数menu3(menu[inp])的外面跳,直到不嵌套而退出
menu3(menu)
三级菜单函数

 三级菜单堆栈实现

l=[menu]
while l:
    for key in l[-1]:
        print(key)
    inp=input('>>>').strip()
    if inp in l[-1]:
        l.append(l[-1][inp])
        print(l)
    elif inp=='b':l.pop()
    elif inp=='q':break
View Code
原文地址:https://www.cnblogs.com/mmyy-blog/p/9304052.html