初学Python(三)

一:类

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中


  • 2
    点赞
  • 1
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值