python类的内置方法
[python] 类常用的内置方法
内置方法 | 说明 |
__init__(self,...) | 初始化对象,在创建新对象时调用 |
__del__(self) | 释放对象,在对象被删除之前调用 |
__new__(cls,*args,**kwd) | 实例的生成操作 |
__str__(self) | 在使用print语句时被调用 |
__getitem__(self,key) | 获取序列的索引key对应的值,等价于seq[key] |
__len__(self) | 在调用内联函数len()时被调用 |
__cmp__(stc,dst) | 比较两个对象src和dst |
__getattr__(s,name) | 获取属性的值 |
__setattr__(s,name,value) | 设置属性的值 |
__delattr__(s,name) | 删除name属性 |
__getattribute__() | __getattribute__()功能与__getattr__()类似 |
__gt__(self,other) | 判断self对象是否大于other对象 |
__lt__(slef,other) | 判断self对象是否小于other对象 |
__ge__(slef,other) | 判断self对象是否大于或者等于other对象 |
__le__(slef,other) | 判断self对象是否小于或者等于other对象 |
__eq__(slef,other) | 判断self对象是否等于other对象 |
__call__(self,*args) | 把实例对象作为函数调用 |
__init__():
__init__方法在类的一个对象被建立时,马上运行。这个方法可以用来对你的对象做一些你希望的初始化。注意,这个名称的开始和结尾都是双下划线。
代码例子:
#!/usr/bin/python # Filename: class_init.py class Person: def __init__(self, name): self.name = name def sayHi(self): print 'Hello, my name is', self.name p = Person('Swaroop') 输出: |
__new__():
__new__()在__init__()之前被调用,用于生成实例对象.利用这个方法和类属性的特性可以实现设计模式中的单例模式.单例模式是指创建唯一对象吗,单例模式设计的类只能实例化一个对象.
#!/usr/bin/python # -*- coding: UTF-8 -*- class Singleton(object): def __init__(self): def __new__(cls, *args, **kwd): # 在__init__之前调用 |
__getattr__()、__setattr__()和__getattribute__():
当读取对象的某个属性时,python会自动调用__getattr__()方法.例如,fruit.color将转换为fruit.__getattr__(color).当使用赋值语句对属性进行设置时,python会自动调用__setattr__()方法.
__getattribute__()的功能与__getattr__()类似,用于获取属性的值.但是__getattribute__()能提供更好的控制,代码更健壮.注意,python中并不存在__setattribute__()方法.
代码例子:
#!/usr/bin/python # -*- coding: UTF-8 -*- class Fruit(object): def __setattr__(self, name, value): if __name__ == "__main__": |
__getitem__():
如果类把某个属性定义为序列,可以使用__getitem__()输出序列属性中的某个元素.假设水果店中销售多钟水果,可以通过__getitem__()方法获取水果店中的没种水果
代码例子:
#!/usr/bin/python # -*- coding: UTF-8 -*- class FruitShop: if __name__ == "__main__": 输出为: |
__str__():
__str__()用于表示对象代表的含义,返回一个字符串.实现了__str__()方法后,可以直接使用print语句输出对象,也可以通过函数str()触发__str__()的执行.这样就把对象和字符串关联起来,便于某些程序的实现,可以用这个字符串来表示某个类
代码例子:
#!/usr/bin/python # -*- coding: UTF-8 -*- class Fruit: if __name__ == "__main__": |
__call__():
在类中实现__call__()方法,可以在对象创建时直接返回__call__()的内容.使用该方法可以模拟静态方法
代码例子:
#!/usr/bin/python # -*- coding: UTF-8 -*- class Fruit: class Growth: # 内部类 def __call__(self): print "grow ..." grow = Growth() # 调用Growth(),此时将类Growth作为函数返回,即为外部类Fruit定义方法grow(),grow()将执行__call__()内的代码 if __name__ == '__main__': fruit = Fruit() fruit.grow() # 输出结果:grow ... Fruit.grow() # 输出结果:grow ...
补充:
python类对象(包括类实例的方法,属性等 成员在内),都是存储在__dict__中。
我们还可以通过重载 __getattr__,_getattribute__ 和 __setattr__ 来拦截对成员的访问。
需要注意的是:
__getattr__ 只有在访问不存在的成员时才会被调用。
__setattr__无论赋值成员是否已经存在,都会被调用。
__getattribut__的代码中,不可调用self,否则将形成死循环。如果需要,则调用它的父类的__getattribut__()方法 : object.__getattribute__(self, item)
示例:
class Class1: def __getattr__(self, name): print ("__getattr__") return def __setattr__(self, name, value): print ("__setattr__") self.__dict__[name] = value
a = Class1() a.x # x不是实例对象a的成员,此时会调用__getattr__ # out: __getattr__ a.x = 123 # 要赋值的x不论是否存在,都会调用__setattr__ # out: __setattr__ a.x # x成员存在时,不会调用__getattr__ # out: 123 a.x = 234 # 要赋值的x不论是否存在,都会调用__setattr__ # out: __setattr__
posted on 2019-02-23 10:28 myworldworld 阅读(167) 评论(0) 收藏 举报