原链接http://blog.fishc.com/category/python
Python 要求必须要有实例才能够被调用
类中定义的属性都是静态属性,相当于c语言的static,如果属性的名字和方法相同 属性会把方法覆盖掉
类中定义的属性和方法都是静态变量,就算类对象被删除了,它们依然存放在内存中的,C语言中,static生命的变量只有在程序退出的时候,这个变量才会被释放
>>> class CC:
def setXY(self,x,y):
self.x = x
self.y = y
def printXY(self):
print(self.x, self.y)
>>> dd.CC()
>>> dd = CC()
>>> dd.setyXY #我现在把CC()删除 那么dd还能被调用么?看上述文字
结果如下 dd还能被调用
>>> del CC
>>> ee = CC()
Traceback (most recent call last):
File "<pyshell#22>", line 1, in <module>
ee = CC()
NameError: name 'CC' is not defined
>>> dd.printXY() #类中定义的属性和方法都是静态变量,就算类对象被删除了,它们依然存放在内存中的,C语言中,static生命的变量只有在程序退出的时候,这个变量才会被释放
2 2
>>>
http://www.jb51.net/article/49402.htm这篇文章中关于类、势力、静态,做了一个简单易懂的区分
二:内置函数
2.1:issubclass
issubclass(clas, classinfo) #classinfo可以是类对象组成的元组,只要class与其中任何一个候选类的子类,则返回True
>>> class A:
pass
>>> class B:
pass
>>> issubclass(B, A) #这里A和B没有关系 所以返回false
False
>>> issubclass(B,B) #这里B和B有关系,返回true
True
>>> issubclass(B, object) #这里同样有关系
True
>>> class C:
pass
>>> issubclass(B, C)
False
>>>
>>> b = B()
>>> isinstance(b, B)
True
>>> isinstance(b, A)
False
>>>
2.3: hasattr(object, name) 检查一个对象里面是不不是有指定的属性
>>> class C:
def __init__(self, x= 0):
self.x = x
>>> c = C()
>>> hasattr(c, 'x')
True
>>> hasattr(c, 'X')
False
>>>
2.4: getattr(object, name[, default]) : 返回对象指定的属性值, 如果指定的参数不存在,则会返回你写如的default
接上
>>> getattr(c, 'x')
0
>>> getattr(c, 'X')
Traceback (most recent call last):
File "<pyshell#51>", line 1, in <module>
getattr(c, 'X')
AttributeError: 'C' object has no attribute 'X'
>>> getattr(c, 'X', '您所访问的属性不存在')
'您所访问的属性不存在'
>>>
2.5:setattr(object, name, value) :跟getattr相对,会返回对象中执行的属性值,如果不存在,则创建一个新的属性并给它赋值
>>> setattr(c, 'y', '哈哈')
>>> getattr(c, 'y')
'哈哈'
>>>
2.6: delattr(object, name) : 用于删除对象中指定的属性,如果属性不存在,则抛出error异常
>>> delattr(c, 'y')
>>> getattr(c, 'y')
Traceback (most recent call last):
File "<pyshell#56>", line 1, in <module>
getattr(c, 'y')
AttributeError: 'C' object has no attribute 'y'
>>>
2.7:property(fget: None, fSet: None, fdel : None, doc : None) 作用:是设置一个属性,属性的作用就是代替一个已经定义好的属性,但是需要传入必须写好的方法
>>> class C:
def __init__(self, size = 10):
self.size = size
def getSize(self):
return self.size
def setSize(self, value):
self.size = value
def delSize(self):
del self.size
x = property(getSize, setSize, delSize) #这里成功的赋值给新属性X,程序比较大的时候,想要改一下名字,这个就比较繁琐,原来所有的都需要改,但是用property就不需要了,这个原来的可以不修改,以后的调用新方法
>>> c1 = C()
>>> c1.getSize()
10
>>> c1.x
10
>>> c1.size
10
>>> c1.setSize(122)
>>> c1.x
122
>>> c1.size
122
>>> c1.getSize()
122
>>> del c1.x
>>> c1.size
Traceback (most recent call last):
File "<pyshell#79>", line 1, in <module>
c1.size
AttributeError: 'C' object has no attribute 'size'
>>>
三:构造和析构
魔法方法总是被双下划线包围,例如 __init__ :其他语言的构造方法
魔法方法是面向对象Python的一切,它能够在适当的时候被调用
对象实例化是第一个被调用的方法是new方法,并不是init方法
如果你继承了一个不可变类型,又需要修改,则需要重写魔法方法
>>> class Capstr(str): #这里的str是不可变类型
def __new__(cls, string): #重写new方法
string = string.upper()
return str.__new__(cls, string) #返回:把重写后的字符串变成新的字符串返回
>>> a = Capstr('I thin is ok')
>>> a
'I THIN IS OK'
>>>
__del__ 方法
>>> c1 = C()
wishing__init__方法
>>> c2 = c1
>>> c3 = c2
>>> del c3
>>> del c2
>>> del c1 #当所有的引用都del之后,才会触发C()的del方法,才会启动垃圾回收机制
我是__del__ 方法
>>> c4 = C()
wishing__init__方法
>>> c5 = c4
>>> del c4
>>> del c5
我是__del__ 方法
>>>
四:魔法方法
__add__(self, other) __sub__(self, other) : 也就是函数内部封装好了给你计算的公式,比方调add就是加 调sub就是减
>>> class New_init(int):
def __add__(self, other): #调用了加 方法
return int.__sub__(self, other) #这里自定义返回减的方法
def __sub__(self, other): #这里调用了减的方法
return int.__add__(self, other) # 这里自定义返回加的方法,这是自定义的,正常返回是正好相反的
>> > a = New_init(4)
>>> b = New_init(1)
>>> a + b
3
>>> a - b
5
>>> class New_init(int): #这里做一下修改
def __add__(self, other):
return self + other #加减法 看看能不能调用自己相加减
def __sub__(self, other):
return self - other #同样这里是
>> > a = New_init(4)
>>> b = New_init(1)
>>> a + b # 这里就会产生无数递归,因为上面的self,代表自己,other:代表另外一个,如果是这样,那么就又是调用自己的加号方法,又调用一遍自己,如此循环,所以应该这么写
>>> class New_init(int): #这里做一下修改
def __add__(self, other):
return int(self) + int(other) #这里需要用值相加
def __sub__(self, other):
return int(self) - int(other) #同样这里是
>> > a = New_init(4)
>>> b = New_init(1)
这样这里就正常了