1.
*attr()
class myClass(object):
def __init__(self):
self.foo = 100
myInst = myClass()
print hasattr(myInst, 'foo')
print getattr(myInst, 'foo')
print hasattr(myInst, 'bar')
#print getattr(myInst, 'bar')
print getattr(myInst, 'bar', 'oooops')
setattr(myInst, 'bar', 'my attr')
print getattr(myInst,'bar')
print dir(myInst)
print getattr(myInst,'bar')
delattr(myInst, 'foo')
print dir(myInst)
2.
字符串在Python内部的表示是unicode编码,因此,在做编码转换时,通常需要以unicode作为中间编码,即先将其他编码的字符串解码(decode)成unicode,再从unicode编码(encode)成另一种编码。
decode的作用是将其他编码的字符串转换成unicode编码,如str1.decode(‘gb2312′),表示将gb2312编码的字符串str1转换成unicode编码。
encode的作用是将unicode编码转换成其他编码的字符串,如str2.encode(‘gb2312′),表示将unicode编码的字符串str2转换成gb2312编码。
3.
iter, next
#! /usr/bin/env python
from random import choice
class RandSeq(object):
"""docstrinRandSeq"""
def __init__(self, seq):
self.data = seq
def __iter__(self):
return self
def next(self):
return choice(self.data)
class AnyIter(object):
def __init__(self, data, safe = False):
self.safe = safe
self.iter = iter(data)
def __iter__(self):
return self
def next(self, howmany = 1):
retval = []
for eachItem in range(howmany):
try:
retval.append(self.iter.next())
except StopIteration:
if self.safe:
break
else:
raise
return retval
if __name__ == '__main__':
'''
for eachItem in RandSeq(('Rock', 'Paper', 'Scissors')):
print eachItem
'''
a = AnyIter(range(10))
i = iter(a)
for j in range(1,5):
print j,' : ', i.next(j)
4.
a.__dict__['x'], then type(a).__dict__['x']
5.
def __getattribute__(self, key):
"Emulate type_getattro() in Objects/typeobject.c"
v = object.__getattribute__(self, key)
if hasattr(v, '__get__'):
return v.__get__(None, self)
return v
'''
The important points to remember are:
descriptors are invoked by the __getattribute__() method
overriding __getattribute__() prevents automatic descriptor calls
__getattribute__() is only available with new style classes and objects
object.__getattribute__() and type.__getattribute__() make different calls to __get__().
data descriptors always override instance dictionaries.
non-data descriptors may be overridden by instance dictionaries.
'''
6.
x.foo
'''
type(x).__dict__['foo'].__get__(x, type(x))
'''
7.
class Property(object):
"Emulate PyProperty_Type() in Objects/descrobject.c"
def __init__(self, fget=None, fset=None, fdel=None, doc=None):
self.fget = fget
self.fset = fset
self.fdel = fdel
if doc is None and fget is not None:
doc = fget.__doc__
self.__doc__ = doc
def __get__(self, obj, objtype=None):
if obj is None:
return self
if self.fget is None:
raise AttributeError("unreadable attribute")
return self.fget(obj)
def __set__(self, obj, value):
if self.fset is None:
raise AttributeError("can't set attribute")
self.fset(obj, value)
def __delete__(self, obj):
if self.fdel is None:
raise AttributeError("can't delete attribute")
self.fdel(obj)
def getter(self, fget):
return type(self)(fget, self.fset, self.fdel, self.__doc__)
def setter(self, fset):
return type(self)(self.fget, fset, self.fdel, self.__doc__)
def deleter(self, fdel):
return type(self)(self.fget, self.fset, fdel, self.__doc__)