推导式生成列表
a = [[3 * i + 1 + j for j in range(3)] for i in range(3)]
print(a) #[[1, 2, 3], [4, 5, 6], [7, 8, 9]]
b = [[4 * i + 1 + j for j in range(4)] for i in range(4)]
print(b) #[[1, 2, 3, 4], [5, 6, 7, 8], [9, 10, 11, 12], [13, 14, 15, 16]]
c = [[4 * i + 1 + j for j in range(4)] for i in range(3)]
print(c) #[[1, 2, 3, 4], [5, 6, 7, 8], [9, 10, 11, 12]]
d = [[3 * i + 1 + j for j in range(3)] for i in range(4)]
print(d) #[[1, 2, 3], [4, 5, 6], [7, 8, 9], [10, 11, 12]]
a = [1,2,3,4,5]
b = list(filter(lambda i:True if i % 2 == 0 else False,a))
print(b) #[2, 4]
面向对象
类(Class): 用来描述具有相同的属性和方法的对象的集合。它定义了该集合中每个对象所共有的属性和方法。对象是类的实例。
方法:类中定义的函数。
对象:通过类定义的数据结构实例。对象包括两个数据成员(类变量和实例变量)和方法。
实例化:创建一个类的实例,类的具体对象。
定义类:
class Dog():
def eat(self):
print("要吃饭")
def sleep(self):
print("要睡觉")
#创建对象
my_dog = Dog()
my_dog.eat() #要吃饭
my_dog.sleep() #要睡觉
#再创建一个对象
tom_dog = Dog()
tom_dog.eat() #要吃饭
tom_dog.sleep() #要睡觉
self变量:哪个对象调用方法或者属性,self就是那个变量
class Person():
def eat(self):
print("eat方法中的self:",id(self))
def sleep(self):
print("sleep方法中的self:",id(self))
a = Person()
a.eat() #eat方法中的self: 36140536
a.sleep() #sleep方法中的self: 36140536
print("对象的id:",id(a)) #对象的id: 36140536
print()
b = Person()
b.eat() #eat方法中的self: 36140592
b.sleep() #sleep方法中的self: 36140592
print("对象的id:",id(b)) #对象的id: 36140592
init():内置方法,在创建对象的时候自动执行
class Cat():
def __init__(self,name,age,sex): #需要self传递的参数name,age,sex
self.name = name #初始化name的值
self.age = age #初始化age的值
self.sex = sex #初始化sex的值
def introduce(self):
print("{},今年{}岁,是只{}猫".format(self.name,self.age,self.sex))
my_cat = Cat("骚包",1,"公")
my_cat.introduce() #骚包,今年1岁,是只公猫
tom_cat = Cat("肥仔",1,"公")
tom_cat.introduce() #肥仔,今年1岁,是只公猫
在类的外部增加属性
class Person():
def dayin(self):
print("我叫{}".format(self.name))
zs = Person()
zs.name = "张三" #类里面没有这个变量,添加进去
zs.dayin() #我叫张三
class Student():
def __init__(self):
pass
def hs1(self,x):
self.name = x
def show(self):
print(self.name)
zs = Student()
zs.name = "张三"
zs.show() #张三
定义一个类,定义两个方法,计算圆的周长和面积:
from math import pi
class Circle():
def __init__(self,radius):
self.radius = radius
def zhouchang(self):
return 2 * pi *self.radius
def area(self):
return pi * self.radius * self.radius
yuan1 = Circle(2)
print(yuan1.zhouchang())
print(yuan1.area())
yuan2 = Circle(4)
print(yuan2.zhouchang())
print(yuan2.area())
#类变量:类变量在整个实例化的对象中是公用的。类变量定义在类中且在函数体之外。类变量通常不作为实例变量使用。
#局部变量:定义在方法中的变量,只作用于当前实例的类。
#实例变量:在类的声明中,属性是用变量来表示的。这种变量就称为实例变量,是在类声明的内部但是在类的其他成员方法之外声明的。
class Stu():
count = 0 #类变量,写在方法外面的,都是类变量
Tax = 0.02
def __init__(self,name):
self.name = name #带self的,都是对象变量
Stu.count += 1
当类属性和对象属性重名的时候,优先使用对象属性
print(Stu.count) #0
a = Stu("张三")
print(Stu.count) #1
b = Stu("李四")
print(Stu.count) #2
print(a.count,Stu.count) #2 2
# a.count = 1000
# print(a.count,Stu.count) #1000 2
a.count = a.count + 1 #a.countd调用Stu.count的值
print(a.count,Stu.count) #3 2
del a.count
print(a.count) #2
class X():
def __init__(self,name,age):
self.name = name
self.age = age
zs = X("张三",18)
print(zs.name) #张三
del zs.name
print(zs.age) #18
print(zs.name) #AttributeError: 'X' object has no attribute 'name'
当类变量为可变数据时:
class Stu():
ls = [1,2,3]
def __init__(self,name):
self.name = name #带self的,都是对象属性
a = Stu("张三")
print(a.ls,Stu.ls) #[1, 2, 3] [1, 2, 3]
a.ls.append(4)
Stu.ls.append(5)
print(a.ls,Stu.ls) #[1, 2, 3, 4, 5] [1, 2, 3, 4, 5]
实例方法/对象方法:在类中定义的普通方法
静态方法:使用@staticmethod定义的,一般用于个类对象以及实例对象无关的代码
类方法:用@classmethod定义的,当一个方法中只涉及到静态属性的时候,可以使用类方法
class Student():
count = 9
def __init__(self,name,age):
self.name = name
self.age = age
@classmethod
def cM(cls):
print(cls.count)
@staticmethod
def hehe(a,b):
print(a + b)
print("我跟你们没关系,只不过在其他方式写不合适")
a = Student("张三",18)
print(Student.count) #9
class Teacher():
count = 0
Tax = 0.002
def __init__(self,name,salary):
self.name = name
self.salary = salary
Teacher.count += 1
@classmethod
def dayin(cls):
print(cls.count)
@staticmethod
def money(a,b):
money = (a + b)*Teacher.Tax
print(money)
a = Teacher("张三",10000)
b = Teacher("李四",8000)
Teacher.dayin()
Teacher.money(a.salary,b.salary)
str():在打印对象的名称时默认调用的是__str__()方法,默认返回的是对象的内存地址。
class Stu():
def __init__(self,name,age):
self.name = name
self.age = age
def __str__(self):
return "我叫{},今年{}".format(self.name,self.age)
a = Stu("张三",19)
print(a) #我叫张三,今年19
b = str(a)
print(b) #我叫张三,今年19
作业
1、邓超 体重 75.0 公斤
邓超每次 奔跑 会减肥 0.1 公斤
邓超每次 吃东西 体重增加 0.5 公斤
class Person():
def __init__(self,name,weight):
self.name = name
self.weight = weight
def __str__(self):
return "我是{},体重{}".format(self.name,self.weight)
def run(self):
self.weight -= 0.1
print(self.weight)
def eat(self):
self.weight += 0.5
print(self.weight)
dc = Person("邓超",75)
dc.run()
dc.eat()
print(dc)
2、
# 需求
#狙击手 麦克 有一把 狙M99
#麦克 可以 开火
#狙M99 能够 发射 子弹
#狙M99 装填 装填子弹 —— 增加子弹数量
class Gun():
def __init__(self,model):
self.model = model
self.bullet_count = 0
def add_bullet(self,count):
self.bullet_count += count
def shoot(self):
if self.bullet_count <= 0:
print("没有子弹了!")
return
self.bullet_count -= 1
print("{}发射一颗子弹,还剩{}颗子弹".format(self.model,self.bullet_count))
class Sniper():
def __init__(self,name):
self.name = name
self.gun = None
def fire(self):
if self.gun == None:
print("{}还没有枪".format(self.name))
print("冲啊...{}".format(self.name))
self.gun.add_bullet(50)
self.gun.shoot()
m99 = Gun("M99")
sniper = Sniper("Mac")
sniper.gun = m99
sniper.fire()
3、静态方法类方法使用
class Game(object):
# 历史最高分
top_score = 0
def __init__(self, player_name):
self.player_name = player_name
@staticmethod
def show_help():
print("提示信息:小心让僵尸吃掉你的脑子哦")
@classmethod
def show_top_score(cls):
print("历史记录 %d" % cls.top_score)
def start_game(self):
print("%s 开始游戏啦..." % self.player_name)
# 1. 查看游戏的帮助信息
Game.show_help()
# 2. 查看历史最高分
Game.show_top_score()
# 3. 创建游戏对象
game = Game("小明")
game.start_game()