A = 100 # 全局变量
def func1():
print("func1方法被调用了")
print(A)
def func2():
print("func2方法被调用了")
# 没有在main方法中 所以当前模块被其他模块引用时会被执行
func1()
# python中的main方法
if __name__ == '__main__':
func2()
func1方法被调用了
100
func2方法被调用了
# 导入其他模块
# python在导入模块时会将模块中的代码执行一次(除main方法中的代码)
from pypac1 import moduleA as mA
'''命名空间
A = 100 # 全局变量
def func1():
print("func1方法被调用了")
print(A)
def func2():
print("func2方法被调用了")
# 没有在main方法中 所以当前模块被其他模块引用时会被执行
func1()
# python中的main方法
if __name__ == '__main__':
func2()
'''
# moduleA.func1()
#print(moduleA.A)
print(mA.A)
mA.func1()
mA.func2()
func1方法被调用了
100
100
func1方法被调用了
100
func2方法被调用了
# 类
# 定义一个人类
# 属性:id name age
# 方法
# 面向对象的三大特性:封装,继承,多态
# 封装:1.代码的封装(类中可以定义属性,方法)
# 2.安全性上的封装(例如java中的private等访问权限修饰符)
class Person:
id = "001"
# name = 'zs'
# age = 20
# 构造方法
# 类中的方法第一个参数都是self 相当于java中的this
def __init__(self, id, name, age):
print(self.id)
# 定义类的属性
# 定义即赋值
self.id = id
self.name = name
self.age = age
# 属性名以__开头 可以实现"封装"
# 但此"封装”并非真正意思上的封装
# 还是可以通过_类名+属性名,获取该属性
self.__secret = 'this is a secret'
# 定义成员方法
# 调用方法时 不需要传入self参数
def eat(self, food):
print("%s can eat %s" % (self.name, food))
# 当对累的对象调用print方法时 会调用此方法
# 类似java的tostring方法
def __str__(self):
return "%s,%s,%s" % (self.id, self.name, self.age)
# 实例化
zs = Person('001', 'zs', 20)
print(zs.id)
print(zs.name)
print(zs.age)
# print(zs.secret)
# print(zs.__secret)
print(zs._Person__secret)
zs.eat('bread')
print("*" * 20)
# 继承
# 定义一个Teacher类
# 属性:id name age subject
# 方法:eat teaching
# 子类继承父类 会自动拥有父类的属性和方法
# 当然在子类中也可以有主见的属性和方法,也能重写父类方法
class Teacher:
def __init__(self, id, name, age, subject):
self.id = id
self.name = name
self.age = age
self.subject = subject
def eat(self, food):
print("Teacher %s can eat %s" % (self.name, food))
def teaching(self):
print("Teacher %s can teach students %s" % (self.name, self.subject))
# 实例化
tea_liu = Teacher('10001', 'liu', 27, 'math')
tea_li = Teacher('10002', 'li', 25, 'Chinese')
print(tea_liu.name)
print(tea_li.name)
tea_liu.teaching()
tea_li.teaching()
print("*" * 20)
# 实现继承
class Teacher(Person):
def __init__(self, id, name, age, subject):
# 使用继承可以减少重复的代码
super(Teacher, self).__init__(id, name, age)
# self.id = id
# self.name = name
# self.age = age
self.subject = subject
# 方法的重写
def eat(self, food):
print("Teacher %s can eat %s" % (self.name, food))
def teaching(self):
print("Teacher %s can teach students %s" % (self.name, self.subject))
# 实例化
tea_liu = Teacher('10001', 'liu', 27, 'math')
tea_li = Teacher('10002', 'li', 25, 'Chinese')
print(tea_liu.name)
print(tea_li.name)
tea_liu.teaching()
tea_li.teaching()
# 会调用父类中实现的__str__方法
print(tea_liu)
print(tea_li)
print("*" * 20)
# 多态
# 父类引用指向子类对象
food = 'beer'
# food = 123
# python在定义变量及函数的参数时可以限定类型
# 主要用于pycharm的提示 并不会用于运行时的代码检查
def eatTwice(food: str):
print(food)
print(food)
eatTwice(food)
print("*"*20)
def eatTwice(person: Person):
food = "rice"
person.eat(food)
person.eat(food)
eatTwice(zs)
#多态
eatTwice(tea_liu)
eatTwice(tea_li)
001
001
zs
20
this is a secret
zs can eat bread
********************
liu
li
Teacher liu can teach students math
Teacher li can teach students Chinese
********************
001
001
liu
li
Teacher liu can teach students math
Teacher li can teach students Chinese
10001,liu,27
10002,li,25
********************
beer
beer
********************
zs can eat rice
zs can eat rice
Teacher liu can eat rice
Teacher liu can eat rice
Teacher li can eat rice
Teacher li can eat rice
i = 10
j = 0
# ZeroDivisionError 除零异常
# print(i/j)
print(i)
print(j)
# NameError 名称异常
# print(k)
# TypeError 类型异常
# print(1+'2')
# valueError 值异常
# int('abc')
list1 = [1, 2, 3, 4, 5]
print(list1[0])
print(list1[1])
print(list1[2])
print(list1[3])
print(list1[4])
# IndexError 索引异常
# print(list1[5])
# AttributeError 属性异常
# list1.split()
# 自定义异常
class AgeError(Exception):
pass
# 处理异常
try:
# 当年龄<0或者>200时 抛出一个AgeError
age = 20
if age < 0 or age > 200:
# 手动抛出
raise AgeError("年龄错误")
print(10 / 0)
# 注意异常捕获的先后顺序
except Exception as e:
print(e)
print("Exception是所有异常类的父类,所以后面捕获异常的代码都会被忽略")
except AgeError as e:
print(e)
except ZeroDivisionError as e:
print(e)
print("请检查除数!")
else:
print("当没有异常发生时则会执行")
finally:
print("不管有没有发生异常都会执行")
10
0
1
2
3
4
5
division by zero
Exception是所有异常类的父类,所以后面捕获异常的代码都会被忽略
不管有没有发生异常都会执行