python面向对象05
"""开空间 调用__init__"""
# class A:pass
# class B(A):pass
# print(B.__bases__)
# 绑定方法和普通函数
from types import FunctionType, MethodType
# FunctionType:函数
# MethodType:方法
# class A:
# def func(self):
# print('infunc')
# a = A()
# print(A.func) # 函数
# print(a.func) # 方法
# print(isinstance(a.func,FunctionType)) #False
# print(isinstance(a.func,MethodType)) #True
# print(isinstance(A.func,FunctionType)) #True
# print(isinstance(A.func,MethodType)) #False
# isinstance type两者对比
# a = 1
# b = 'abc'
# print(isinstance(a,int)) # True
# print(isinstance(a,float)) # Fasle
# print(isinstance(b,str)) # True
# a = 1
# b = 'abc'
# print(type(a) is int) # True
# print(type(b) is str) # True
# class Cat:
# pass
#
# xiaobai = Cat()
# print(type(xiaobai) is Cat) # True
# print(isinstance(xiaobai, Cat)) # True
# class Animal: pass
# class Cat(Animal): pass
# xiaobai = Cat()
# print(type(xiaobai) is Cat) # True
# print(type(xiaobai) is Animal) # False
# print(isinstance(xiaobai, Cat)) # True
# print(isinstance(xiaobai, Animal)) # True
# 类中各种方法
# class A:
# name = "name-A"
# def func1(self):pass
# def func2(self):pass
#
# class B:pass
# class C(B,A):pass
# print(A.__bases__)#(<class 'object'>,)
# print(C.__base__) #<class '__main__.B'>
# print(C.__bases__) #(<class '__main__.B'>, <class '__main__.A'>)
# print(A.__dict__) #查看类中所有存在的变量和属性
# print(A.__name__) # 查看类的名称
# print(A.__class__) #<class 'type'> 查看类的类型,大部分类的类型都是type
# print(B.__class__) #<class 'type'>
# print(C.__class__) #<class 'type'>
# print(C.__module__)
# import module
# __doc__ 文档说明
# class A:
# '''
# 这个类是用来操作A的所有操作
# '''
# pass
# print(A.__doc__) # 这个类是用来操作A的所有操作
class Curse(object):
def __init__(self, name, proid, price):
self.name = name
self.proid = proid
self.price = price
python = Curse('python', '6 month', '21000')
go = Curse('go', '4 month', '12000')
import pickle
# 写入文件
# with open('pickle_file','wb') as f:
# 格式 pickle.dump(对象名,文件句柄)
# pickle.dump(python,f)
# 读取文件
# with open('pickle_file','rb') as f:
# python = pickle.load(f)
# print(python) #<__main__.Curse object at 0x0000019465122978
# print(python.__dict__) #{'name': 'python', 'proid': '6 month', 'price': '21000'}
# print(python.name) #python
# print(python.price) #21000
#多个对象时的读取
import pickle
#写入文件
# with open('pickle1_file','wb') as f:
# pickle.dump(python,f)
# pickle.dump(go,f)
#读取文件
with open('pickle1_file','rb') as f:
while True:
try:
obj = pickle.load(f)
print(obj.name,obj.price)
# print(obj.__dict__)
except EOFError:
break
欢迎转载,但要标明出处.否则追究.欢迎大家阅读收藏和评论,会更新很多精粹。