面向对象的类成员(特殊方法)
isinstance(对象,类名) #判断该对象是否是该类及其父类的实例
issubclass(类名,类名) #判断前类是否是后类的子类
面向对象的类成员:1、方法 2、属性 3、字段
字段:
普通保存在对象中
静态保存在类中
class Foo:
cc = 123 #静态字段
def __init__(self):
self.name = "Guido" #字段,(普通字段)
def show(self):
print(self.name)
class province:
country = "中国" #静态字段在内存中只保存一份就行,在代码加载时已经创建。
def __init__(self, name):
self.name = name
#一般情况自己访问自己的字段,普通字段只可以通过对象访问。
hn = province("河南")
hb = province("河北")
print(hn.name) #用对象访问
print(province.country) #用类访问
print(hn.country) #通过对象也可以访问静态字段(不到万不得已不要用)
class province:
country = "中国"
def __init__(self, name):
self.name = name
def f2(self, name):
print("cls")
print(name)
ret = province("Guido")
ret.f2(ret.name) #(对象.传入的参数变量)才能使用__init__里的参数。
#执行结果:
cls
Guido
静态方法:
class province:
country = "中国"
def __init__(self, name):
self.name = name
# 普通方法由对象去调用执行(方法属于类)
def show(self):
print(123)
print(self.name)
ret = province("Guido")
ret.show()
class province:
country = "中国"
def __init__(self, name):
self.name = name
#静态方法
@staticmethod
def f1(arg1, arg2, arg3): #可以无参数
print(arg1, arg2, arg3) #静态方法,由类调用执行,相当于属于该类的函数
ret = province("Guido")
province.f1(11, province.country, ret.name)
class province:
country = "中国"
def __init__(self, name):
self.name = name
#类方法(属于静态方法)
@classmethod
def f2(cls): #class至少有一个参数cls
print(cls) #cls是类名,cls()创建对象
province.f2() #不用传实际参数,cls自动传入类名
属性:
不伦不类的东西,具有方法的写作形式,且具有字段的访问形式。
class pager:
def __init__(self, all_count):
self.all_count = all_count
def all_pager(self):
a1, a2 = divmod(self.all_count, 10) #分页
if a2 == 0:
return a1
else:
return a1+1
p = pager(101)
print(p.all_count) #执行字段
result = p.all_pager() #执行方法
print(result)
class pager:
def __init__(self, all_count):
self.all_count = all_count
@property
def all_pager(self):
a1, a2 = divmod(self.all_count, 10) #分页
if a2 == 0:
return a1
else:
return a1+1
p = pager(101)
ret = p.all_pager #利用属性执行@property在下面的方法
print(ret)
class pager:
def __init__(self, all_count):
self.all_count = all_count
@property
def all_pager(self):
a1, a2 = divmod(self.all_count, 10) #分页
if a2 == 0:
return a1
else:
return a1+1
@all_pager.setter
def all_pager(self, value):
print(value)
@all_pager.deleter
def all_pager(self):
print("del all_pager")
p = pager(101)
print(p.all_count)
p.all_count = 102
del p.all_count
p.all_pager = 111 #利用属性执行@all_pager.setter下面的方法
del p.all_pager #利用属性执行@all_pager.deleter下面的方法
以上@的方式只是提供一种上下的对应关系。
属性的另一种方式:
class pager:
def __init__(self, all_count):
self.all_count = all_count
def f1(self):
return 123
def f2(self, value):
print(value)
def f3(self):
print("jjjj")
foo = property(fget = f1, fset = f2, fdel = f3)
p = pager(101)
result = p.foo #对应执行f1
print(result)
p.foo = "Guido" #对应执行f2
del p.foo #对应执行f3
特殊的类成员:
class Foo:
#构造方法(但对象建立成功就会立刻执行此方法)
def __init__(self, name, age):
self.name = name
self.age = age
#析构方法(在垃圾回收机制执行前执行)(在python程序结束前会自动调用__del__方法)
def __del__(self):
pass
def __call__(self):
print("call")
#用print打印对象就会自动执行__str__方法
def __str__(self):
return "%s - %d" %(self.name, self.age)
obj = Foo()
obj() #对象() 执行call
Foo()
obj1 = Foo("Guido",22)
obj2 = Foo("alex",23)
print(obj1)
print(obj2)
ret = str(obj1) #无__str__方法,该操作不对内部进行字符串的转换
print(ret) #用print打印对象就会自动执行__str__方法
class Foo:
def __init__(self, name, age):
self.name = name
self.age = age
def __add__(self, other):
temp = "%s - %d" %(self.name, other.age) #__init__里封装的参数
return temp
obj1 = Foo("alex", 34)
obj2 = Foo("eric",23)
ret = obj1+obj2 #obj1+obj2 return temp, temp = ret
print(ret)
#获取对象中封装的数据
ret = obj1.__dict__
print(ret)
print(Foo.__dict__)
def __getitem__(self, item):
print(item)
return 123
def __setitem__(self, key,value):
print("setitem")
def __delitem__(self, key):
print("delitem")
obj = Foo("alex", 32)
#语法对应关系
ret = obj["ad"] #执行__getitem__
print(ret)
obj["K1"] = 11 #执行__setitem__
del obj["K1"] #执行__delitem__
ret1 = obj[1:4] #执行__getitem__
class Foo:
def __init__(self, name, age):
self.name = name
self.age = age
def __getitem__(self, item):
print(type(item),item)
print(item.start, item.stop, item.step)
return 123
def __setitem__(self, key, value):
print(key.start, key.stop, key.step)
print(type(key),type(value))
def __delitem__(self, key):
print(key.start, key.stop, key.step)
print(type(key))
obj = Foo("alex", 22)
ret = obj[1:4:2] #执行__getitem__
obj[1:4] = [11,22,33,44,55] #执行__setitem__
del obj[1:4] #执行__delitem__
#执行结果:
(<type 'slice'>, slice(1, 4, 2))
(1, 4, 2)
(1, 4, None)
(<type 'slice'>, <type 'list'>)
(1, 4, None)
<type 'slice'>
class Foo:
pass
obj = Foo()
for item in obj: #报错(对象默认是不可迭代的)
print(item)
class Foo:
def __iter__(self): #就是一个生成器
yield 1 #还可以return iter([11,22,33,44])
yield 2
obj = Foo()
for item in obj:
print(item)
#执行结果:
1
2


浙公网安备 33010602011771号