变量的赋值操作
只是形成两个对象,实际上还是指向同一个对象
浅拷贝
子对象内容不拷贝,只拷贝源对象
class CPU:
pass
class Disk:
pass
class Computer:
def __init__(self,cpu,disk):
self.cpu=cpu
self.disk=disk
#变量的赋值操作
cpu1=CPU()
cpu2=cpu1
print(cpu1)
print(cpu2)
#浅拷贝
disk=Disk()#创建一个硬盘类的对象
computer=Computer(cpu1,disk)#创建一个计算机类的对象
import copy
computer2=copy.copy(computer)
print(computer,computer.cpu,computer.disk)
print(computer2,computer2.cpu,computer2.disk)
深拷贝
使用copy模块的deepcopy函数,递归拷贝对象中包含的子对象,源对象和拷贝对象所有的子对象也不相同
print(computer,computer.cpu,computer.disk)
print(computer3,computer3.cpu,computer3.disk)
模块的使用
import math
print(id(math))
print(type(math))
print(math)
print(math.pi)
print(dir(math))#pow(),ceil(),floor()……
点击make dictionary as 使用自定义的模块
以主程序方式运行
def add(a,b):
return a+b
def div(a,b):
return a/b
if __name__=="__main__":
print(add(10,20))#只有当点击运行calc时,才会执行此句
点击python package新建包
包package和目录dictionary 的区别:
包含__init__.py的目录称为包
不包含__init__.py的目录称为目录
内置模块
import sys
print(sys.getsizeof(24))#获取对象所占的内存大小
print(dir(sys))
import time
print(time.time())
print(time.localtime(time.time()))
import schedule
import time
def job():
print("哈哈————")
schedule.every(3).seconds.do(job)#每3秒执行一次job函数
while True:
schedule.run_pending()#启动
time.sleep(1)#每一次休眠1秒