一:类
1.类的创建
class GameStats():
def __init__(self,ai_settings):
self.ai_settings=ai_settings
self.reset_stats()
self.game_active=True
self.game_active=False
def reset_stats(self):
self.ships_left=self.ai_settings.ship_limit
2.根据类创造实例
my=GAMEStats()
s=my.self.ai_settings
print(s)
my.reset_stats()
3.继承
class Car():
def __init__(self,make,model):
self.make=make
self.model=model
class EelCar(Car):
def __init__(self,make,model):
super().__init__(make,model)
4.重写
class Car():
def __init__(self,make,model):
self.make=make
self.model=model
def pyt(self):
print(“panyutian”)
class EelCar(Car):
def __init__(self,make,model):
super().__init__(make,model)
def pyt():
print("panyutian")
5.导入类
import sys
import pygame
from bullet import Bullet
from alien import Alien
from time import sleep
二:文件与异常
1.读文件
with open('pyt.txt') as file_object:
content = file_object.read()
print(content)
2.写文件
with open("pyt.txt",'w') as file_object:
file_object.write('I love you')
3.附加文件
with open("pyt.txt",'a') as file_object:
file_object.write('I love you')
4.异常
try:
print(5/0)
except ZeroDivisionError:
print("I LOVE YOU")
else:
print("I bu love you")
5json.dump().存储数据
import json
number=[1,2,3,4,5]
filename ="pyt.json"
with open(filename,'w') as f_obj:
json.dump(number,f_obj)
6.json.load()读取数据
import json
filename ="pyt.json"
with open(filename,'w') as f_obj:
context=json.load(f_obj)
print(context)
三:测试函数
1.单元测试(先执行test开头的方法)
import unittest
class Name(unittest.TestCase):
def test_first_last_name(self):
self.assertEqual("pyt","pyt")
unittest.main()
2.setup()方法(先执行setup()方法)
import unittest
class Name(unittest.TestCase):
def test_first_last_name(self):
self.assertEqual("pyt","pyt")
def setUp(self):
self.assertEqual("pyt","pyt")
unittest.main()
3.各种断言方法
assertEqual(a,b) 核实a == b
assertNoEqual(a,b) 核实a != b
assertTrue(x) 核实x为True
assertFalse(x) 核实x为False
assertIn(item,list) 核实item在list中
assertNotIn(item,list) 核实item不在list中