python的魔术方法
2016-05-03 15:56
579 查看
持续更新
类:Example
实例:example = Example()
类:Example
实例:example = Example()
__str__
描述类的实例 print example
__repr__
描述类 print Example
__call__
调用实例 example() #=Example()()
__setattr__
实例属性赋值
__getattr__
获取实例属性值
class Book(object): def __setattr__(self, name, value): if name == 'value': object.__setattr__(self, name, value - 100) else: object.__setattr__(self, name, value) def __getattr__(self, name): try: return object.__getattribute__(name) except: return name + ' is not found!' def __str__(self): return ' cost : ' + str(self.value) c = Book() c.value = 101 print c.value #1 print c print c.Type
__slots__
类所能包含的所有新建属性,继承自object类
相关文章推荐
- python成功配置scikit-learn,含试错历程
- Python file.seek()文件游标移动操作方法
- 使用python抓取豆瓣电影信息
- Python Rate Limiter
- python读conf配置文件--ConfigParser
- Python基础
- python list列表排序的两种方法及实例讲解
- Python实现欧拉计划20题
- Python OS 文件/目录方法
- python and or 的高级用法
- Python的yield协程机制实现生产者-消费者模式
- Python中super关键字的作用
- numpy上手笔记
- Python实现欧拉计划19题
- 125. Valid Palindrome [easy] (Python)
- Mac下配置Caffe的Python接口
- [python]连接两个List
- [Python]元组与列表的区别及内建用法
- python 参数 *
- Python中的除法 整除 非整除