python 格式化、set类型和class类基础知识练习(下)

简介: lambda 参数1,参数2,参数3:表达式 特点: 1.使用lambda关键字创建函数 2.没有名字的函数 3.匿名函数冒号后面的表达式有且只有一个,是表达式不是语句 4.自带return

20 在类的内部,使用def定义的为实例方法,第一个参数为self,实例方法归实例所有



21 添加实例属性



class People:
    name = 'zhan',
    age = 20,
    def eat(self):
        print('正在吃饭')
# 创建对象
people = People()
people.eat()
# 添加属性
people.name2 = 'zhan'
people.age2 = 22


22. 类的__init__()方法


class People:
    # 初始化的操作,实例属性,自动执行
    def __init__(self):
        self.name = 'zhan'
        self.age = 20
    def eat(self):
        print('正在吃饭')
# 创建对象
people = People()
people.eat()


23. 类的__init__()使用参数



class People:
    # 初始化的操作,实例属性,自动执行
    def __init__(self, name, age):
        self.name = name
        self.age = age
    def eat(self,food):
        print(self.name+food)
# 创建对象
people = People('叫我詹躲躲', 20)
people.eat('正在吃饭')
people.eat('洗澡')
people.eat('跑步')


24 理解类的self


# 类似于js里面的this
class Person:
    def eat(self):
        print(id(self))
        pass
    pass
person = Person()
person.eat()
print(id(person)) 
# self和对象指向同一个内存地址,self就是对象的引用
#<__main__.Person object at 0x0000020864815CC0>


25 魔术方法



__init__ :初始化实例属性
__str__ :自定义对象的格式
__new__ :对象实例化
__class__
__del__


class Animal:
    def __str__(self):
        return '3213213123123'
        pass
    pass
animal = Animal()
print(animal) 
class Animal:
    def __str__(self):
        return '3213213123123'
        pass
    pass
    def __new__(cls,*args,**kwargs):
        print("----new执行---")
        return object.__new__(cls) #真正创建对象实例的
        pass
animal = Animal()
print(animal) 
# __new__ 和__init__的区别

__new__ 类的实例化方法,必须返回实例,否则创建不成功

__init__数据属性的初始化工作,认为是实例的构造方法,接受实例化self并对其进行构造


__new__ 至少一个参数是cls,代表要实例化的类

__new__ 执行要比__init__早


26 案例练习 决战紫禁之巅



# 属性:
# name:玩家名称
# blood:血量
# 方法:
# tong() 捅一刀,掉10滴血
# kanren() 砍一刀掉15滴血
# chiyao() 补血10滴血
# __str__打印玩家的状态
class Role:
    def __init__(self,name,blood):
        self.name = name
        self.blood = blood
        pass
    # 砍人
    def tong(self,enemy):
        enemy.blood -=10
        info = '【%s】捅了【%s】一刀'%(self.name,enemy.name)
        print(info)
        pass
    # 砍人
    def kanren(self,enemy):
        enemy.blood -=15
        info = '【%s】砍了【%s】一刀'%(self.name,enemy.name)
        print(info)
        pass
    # 吃药
    def chiyao(self):
        self.blood +=10
        info = '【%s】吃了一口药,增加10滴血'%(self.name)
        print(info)
        pass
    def __str__(self):
        return '%s还剩下%s的血量'%(self.name,self.blood)
xmcx = Role('西门吹雪',100)
ygc = Role('叶孤城',100)
while True:
    if xmcx.blood<=0 or ygc.blood<=0:
        break
    print('*********************')
    xmcx.tong(ygc)
    xmcx.kanren(ygc)  
    print('*********************')
    ygc.tong(xmcx) 
    ygc.chiyao() 
    print('*********************')
    print(xmcx)
    print(ygc)
*********************
【西门吹雪】捅了【叶孤城】一刀
【西门吹雪】砍了【叶孤城】一刀
*********************
【叶孤城】捅了【西门吹雪】一刀
【叶孤城】吃了一口药,增加10滴血
*********************
西门吹雪还剩下50的血量
叶孤城还剩下25的血量
*********************
【西门吹雪】捅了【叶孤城】一刀
【西门吹雪】砍了【叶孤城】一刀
*********************
【叶孤城】捅了【西门吹雪】一刀
【叶孤城】吃了一口药,增加10滴血
*********************
西门吹雪还剩下40的血量
叶孤城还剩下10的血量
*********************
【西门吹雪】捅了【叶孤城】一刀
【西门吹雪】砍了【叶孤城】一刀
*********************
【叶孤城】捅了【西门吹雪】一刀
【叶孤城】吃了一口药,增加10滴血
*********************
西门吹雪还剩下30的血量
叶孤城还剩下-5的血量


27 实例练习1 水果类



class Fruit:
    def __init__(self,name,color):
        self.name = name
        self.color = color
    def showColor(self):
        print('%s的颜色为%s'%(self.name,self.color))
apple = Fruit('苹果','红色').showColor()
orange = Fruit('橘子','黄色').showColor()
watermelen = Fruit('西瓜','绿色').showColor()


28 验证self 就是实例本身



class CkeckSelf:
    def __str__(self):
        print(id(self)) 
        pass
CkeckSelf().__str__()
selfObj = CkeckSelf()
print(id(selfObj))


29 定义animal类,输出所有的属性



class Animal:
    def __init__(self, color, name, age):
        self.color = color
        self.name = name
        self.age = age
    def run(self):
        print('%s在跑步'%(self.name))
        pass
    def eat(self):
        print('%s在吃东西' %(self.name))
        pass
    def __str__(self):
        return '%s岁的%s的%s'%(self.age,self.color,self.name)
cat = Animal('黑色','小猫',2)
dog = Animal('白色','小狗',3)
cat.run()
dog.run()
print(cat)
print(dog)
# 小猫在跑步
# 小狗在跑步
# 2岁的黑色的小猫
# 3岁的白色的小狗
相关文章
|
1月前
|
存储 JavaScript Java
(Python基础)新时代语言!一起学习Python吧!(四):dict字典和set类型;切片类型、列表生成式;map和reduce迭代器;filter过滤函数、sorted排序函数;lambda函数
dict字典 Python内置了字典:dict的支持,dict全称dictionary,在其他语言中也称为map,使用键-值(key-value)存储,具有极快的查找速度。 我们可以通过声明JS对象一样的方式声明dict
108 2
|
2月前
|
Python
Python中的f-string:更优雅的字符串格式化
Python中的f-string:更优雅的字符串格式化
261 100
|
2月前
|
开发者 Python
Python中的f-string:高效字符串格式化的利器
Python中的f-string:高效字符串格式化的利器
357 99
|
2月前
|
Python
Python中的f-string:更优雅的字符串格式化
Python中的f-string:更优雅的字符串格式化
|
2月前
|
开发者 Python
Python f-strings:更优雅的字符串格式化技巧
Python f-strings:更优雅的字符串格式化技巧
|
2月前
|
开发者 Python
Python f-string:高效字符串格式化的艺术
Python f-string:高效字符串格式化的艺术
|
2月前
|
Python
使用Python f-strings实现更优雅的字符串格式化
使用Python f-strings实现更优雅的字符串格式化
|
3月前
|
Python
Python中的f-string:更简洁的字符串格式化
Python中的f-string:更简洁的字符串格式化
249 92
|
4月前
|
PHP Python
Python format()函数高级字符串格式化详解
在 Python 中,字符串格式化是一个重要的主题,format() 函数作为一种灵活且强大的字符串格式化方法,被广泛应用。format() 函数不仅能实现基本的插入变量,还支持更多高级的格式化功能,包括数字格式、对齐、填充、日期时间格式、嵌套字段等。 今天我们将深入解析 format() 函数的高级用法,帮助你在实际编程中更高效地处理字符串格式化。
446 0

推荐镜像

更多