栏目分类:
子分类:
返回
名师互学网用户登录
快速导航关闭
当前搜索
当前分类
子分类
实用工具
热门搜索
名师互学网 > IT > 软件开发 > 后端开发 > Python

Python入门题032:类和函数级静态变量

Python 更新时间: 发布时间: IT归档 最新发布 模块sitemap 名妆网 法律咨询 聚返吧 英语巴士网 伯小乐 网商动力

Python入门题032:类和函数级静态变量

题目:

实现类和函数级的静态变量。

视频教程:

Python入门题032:类和函数级静态变量

代码1:

class People:
    Type = '人'

    def __init__(self, name):
        self.name = name

    def show(self):
        print(f'{self.name} 是 {self.Type}')


class Teacher(People):
    pass


class Student(People):
    pass


teacher_li = Teacher('李老师')
student_ming = Student('小明')
student_hong = Student('小红')

print('------ 1')
teacher_li.show()
student_ming.show()
student_hong.show()

print('------ 2')
People.Type = '僵尸'

teacher_li.show()
student_ming.show()
student_hong.show()

print('------ 3')
People.Type = '人'
Student.Type = '僵尸'

teacher_li.show()
student_ming.show()
student_hong.show()

print('------ 4')
People.Type = '人'

teacher_li.show()
student_ming.show()
student_hong.show()

代码2:

def ticktock():
    if not hasattr(ticktock, 'counter'):
        ticktock.counter = 0
    print(f'欢迎光临,这是你第 {ticktock.counter} 次来了!')
    ticktock.counter += 1


ticktock()
ticktock()
ticktock()
转载请注明:文章转载自 www.mshxw.com
本文地址:https://www.mshxw.com/it/268561.html
我们一直用心在做
关于我们 文章归档 网站地图 联系我们

版权所有 (c)2021-2022 MSHXW.COM

ICP备案号:晋ICP备2021003244-6号