您的位置:首页 > 编程语言 > Python开发

深入学习Django源码基础3 - python提供的对象默认方法

2013-08-31 15:20 996 查看
详细内容看这里A Guide to Python's Magic Methods

这里也有1篇中文的翻译部分https://pycoders-weekly-chinese.readthedocs.org/en/latest/issue6/a-guide-to-pythons-magic-methods.html

把python object提供的默认重载方法做了分类

1:构造和析构

__new__(cls, [...) 创造类

__init__(self,
[...)初始化

__del__(self)删除类

演示代码

from os.path import join

class FileObject:
'''Wrapper for file objects to make sure the file gets closed on deletion.'''

def __init__(self, filepath='~', filename='sample.txt'):
# open a file filename in filepath in read and write mode
self.file = open(join(filepath, filename), 'r+')

def __del__(self):
self.file.close()
del self.file


2:自定义类的操作符比较运算

也就是 == >= 此类

__cmp__(self, other)比较

__eq__(self,
other)==

__ne__(self,
other)!=

__lt__(self,
other)<

__gt__(self,
other)>

__le__(self,
other)<=

__ge__(self,
other)>=

演示代码

class Word(str):
'''Class for words, defining comparison based on word length.'''

def __new__(cls, word):
# Note that we have to use __new__. This is because str is an immutable
# type, so we have to initialize it early (at creation)
if ' ' in word:
print "Value contains spaces. Truncating to first space."
word = word[:word.index(' ')] # Word is now all chars before first space
return str.__new__(cls, word)

def __gt__(self, other):
return len(self) > len(other)
def __lt__(self, other):
return len(self) < len(other)
def __ge__(self, other):
return len(self) >= len(other)
def __le__(self, other):
return len(self) <= len(other)


3:数值运算类

这里不列举

4:描述类

常见

__str__(self)

__repr__(self)

__unicode__(self)

5:属性操作

__getattr__(self, name)

__setattr__(self,
name, value)

__delattr__

6:自定义序列操作

__len__(self)长度

__getitem__(self,
key)

__setitem__(self,
key, value)

__delitem__(self,
key)

__iter__(self)迭代器

__reversed__(self)反向

__contains__(self,
item)

__missing__(self,
key)子类继承时候忽略

演示代码

class FunctionalList:
'''A class wrapping a list with some extra functional magic, like head,
tail, init, last, drop, and take.'''

def __init__(self, values=None):
if values is None:
self.values = []
else:
self.values = values

def __len__(self):
return len(self.values)

def __getitem__(self, key):
# if key is of invalid type or value, the list values will raise the error
return self.values[key]

def __setitem__(self, key, value):
self.values[key] = value

def __delitem__(self, key):
del self.values[key]

def __iter__(self):
return iter(self.values)

def __reversed__(self):
return FunctionalList(reversed(self.values))

def append(self, value):
self.values.append(value)
def head(self):
# get the first element
return self.values[0]
def tail(self):
# get all elements after the first
return self.values[1:]
def init(self):
# get elements up to the last
return self.values[:-1]
def last(self):
# get last element
return self.values[-1]
def drop(self, n):
# get all elements except first n
return self.values[n:]
def take(self, n):
# get first n elements
return self.values[:n]


7:反射

用来检查类型

__instancecheck__(self,
instance)

__subclasscheck__(self, subclass)

8:函数化

__call__(self,
[args...])

9:上下文管理

__enter__(self)

__exit__(self,
exception_type, exception_value, traceback)

10:抽象类

http://docs.python.org/2/library/abc.html

11:绑定的描述符

__get__(self,
instance, owner)

__set__(self,
instance, value)

__delete__(self,
instance)

演示代码

class Meter(object):
'''Descriptor for a meter.'''

def __init__(self, value=0.0):
self.value = float(value)
def __get__(self, instance, owner):
return self.value
def __set__(self, instance, value):
self.value = float(value)

class Foot(object):
'''Descriptor for a foot.'''

def __get__(self, instance, owner):
return instance.meter * 3.2808
def __set__(self, instance, value):
instance.meter = float(value) / 3.2808

class Distance(object):
'''Class to represent distance holding two descriptors for feet and
meters.'''
meter = Meter()
foot = Foot()


12:拷贝

__copy__(self)

__deepcopy__(self,
memodict={})深拷贝

13:存储与提取

__getinitargs__(self)

__getnewargs__(self)

__getstate__(self)

__setstate__(self,
state)

__reduce__(self)

__reduce_ex__(self)

import time

class Slate:
'''Class to store a string and a changelog, and forget its value when
pickled.'''

def __init__(self, value):
self.value = value
self.last_change = time.asctime()
self.history = {}

def change(self, new_value):
# Change the value. Commit last value to history
self.history[self.last_change] = self.value
self.value = new_value
self.last_change = time.asctime()

def print_changes(self):
print 'Changelog for Slate object:'
for k, v in self.history.items():
print '%s\t %s' % (k, v)

def __getstate__(self):
# Deliberately do not return self.value or self.last_change.
# We want to have a "blank slate" when we unpickle.
return self.history

def __setstate__(self, state):
# Make self.history = state and last_change and value undefined
self.history = state
self.value, self.last_change = None, None
内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息
标签: