网站快速排名技术,wordpress采集文章后定时发布,专业做二手房的网站,wordpress去除文章rss1、对象的属性 python一切皆对象#xff0c;每个对象都可能有多个属性。python的属性有一套统一的管理方案。 属性的__dict__系统 对象的属性可能来自于其类定义#xff0c;叫做类属性#xff1b;还可能是该对象实例自身定义的属性#xff0c;叫做对象属性。类属性… 1、对象的属性 python一切皆对象每个对象都可能有多个属性。python的属性有一套统一的管理方案。 属性的__dict__系统 对象的属性可能来自于其类定义叫做类属性还可能是该对象实例自身定义的属性叫做对象属性。类属性可能来自类定义自身也可能根据定义继承而来。 对象的属性存储在对象的__dict__属性中__dict__是一个词典键为属性名值为属性本身。例如 class Bird(object):feather Trueclass Chicken(Bird):fly Falsedef __init__(self, age):self.age agesummer chicken(2)
print Bird.__dict__
print Chicken.__dict__
print summer.__dict__{__dict__: attribute __dict__ of Bird objects, __module__: __main__, __weakref__: attribute __weakref__ of Bird objects, feather: True, __doc__: None}{fly: False, __module__: __main__, __doc__: None, __init__: function __init__ at 0x2b91db476d70}{age: 2}可以看出对类或对象实际类也是一类对象调用__dict__方法只是返回该类或对象新增的属性。如果只有一个对象而不知道它的类以及其他信息的时候可以利用__class__属性找到对象的类然后调用类的__base__属性来查询父类。 可以通过__dict__来获取和设置对象的属性。 summer.__dict__[age] 3
print(summer.__dict__[age])summer.age 5
print(summer.age)使用特殊方法__getattr__ 可以使用 __getattr__(self, name) 来查询即时生成的属性当我们查询一个属性的时候如果通过__dict__方法无法找到该属性那么python会调用该对象的__getattr__方法。 class bird(object):feather Trueclass chicken(bird):fly Falsedef __init__(self, age):self.age agedef __getattr__(self, name):if name adult:if self.age 1.0: return Trueelse: return Falseelse: raise AttributeError(name)summer chicken(2)
print(summer.adult)
summer.age 0.5 #本身有age属性
print(summer.adult) #本省没有age属性会调用__getattr__方法
print(summer.male) #本省没有age属性会调用__getattr__方法抛出异常每个特性需要有自己的处理函数而__getattr__可以将所有的即时生成属性放在同一个函数中处理。__getattr__可以根据函数名区别处理不同的属性。比如上面我们查询属性名male的时候raise AttributeError。python中还有一个__getattribute__特殊方法用于查询任意属性而__getattr__只能用来查询不在__dict__系统中的属性。 2、闭包 函数对象作用域 python中函数也是对象函数对象也有其存活的范围就是函数对象的作用域。函数对象用def语句定义函数对象的作用域与def所在的层级相同。比如在函数A中定义内部函数B内部函数B只能在定义该函数A内部使用不能在函数A外部使用。 def line_conf():def line(x):return 2*x1print(line(5)) # within the scopeline_conf()
print(line(5)) # out of the scope如果使用lambda定义函数那么函数对象的作用域与lambda所在的层级相同。 闭包 函数是一个对象所以可以作为某个函数的返回结果。 def line_conf():b 15def line(x):return 2*xbreturn line # return a function objectb 5
my_line line_conf()
print(my_line(5)) line定义的隶属程序块中引用了高层级的变量b但b信息存在于line的定义之外成b为line的环境变量。line作为line_conf的返回值时line中已经包含了b的取值尽管b并不隶属于line). 一个函数和它的环境变量合在一起就构成了一个闭包。在python中闭包就是一个包含有环境变量取值的函数对象环境变量取值被保存在函数对象的__closure__属性中。比如 def line_conf():b 15def line(x):return 2*xbreturn line # return a function objectb 5
my_line line_conf()
print(my_line.__closure__)
print(my_line.__closure__[0].cell_contents)__closure__里包含了一个元组该元组中的每个元素都是cell类型的对象。 def line_conf(a, b):def line(x):return ax breturn lineline1 line_conf(1, 1)
line2 line_conf(4, 5)
print(line1(5), line2(5))
只需要变换参数a,b就可以获得不同的直线表达函数。由此我们可以看到
闭包也具有提高代码可复用性的作用。3、装饰器 装饰器是一种高级的python语法装饰器可以对一个函数、方法或者类进行加工。 1装饰函数和方法 装饰器经常用于对一些函数添加这些函数需要共同执行的一些操作。 #定义装饰器函数装饰器名任意
def decorator(F):def new_F(a, b):print(input, a, b)return F(a, b)return new_F #返回一个可调用对象该可调用对象以函数为参数# get square sum
decorator #使用装饰器进行装饰在前面使用 装饰器名
def square_sum(a, b):return a**2 b**2# get square diff
decorator
def square_diff(a, b):return a**2 - b**2print(square_sum(3, 4))
print(square_diff(3, 4))
#使用装饰器的效果等同于将函数重定义
square_sum decorator(square_sum)
square_sum(3, 4)2含参的装饰器 装饰器允许我们在调用装饰器的时候提供其他参数比如 decorator(params..) #a new wrapper layer
def pre_str(pre):#old decoratordef decorator(F):def newF(a, b):print (pre intput, a, b)return F(a,b)return newFreturn decoratorpre_str(xxxfjdflsd) #提供装饰器参数
def square_num(a, b):return a**2 b**23装饰类 一个装饰器可以接收一个类并返回一个类达到加工类的效果。 def decorator(aClass):class newClass(object):def __init__(self, age):self.total_display 0self.wrapped aClass(age)def display(self):self.total_display 1print(total display, self.total_display)self.wrapped.display()return newClassdecorator
class Bird(object):def __init__(self, age):self.age agedef display(self):print(my age is, self.age)eagleLord Bird(5)for i in range(3):eagleLord.dislay()参考 http://www.cnblogs.com/vamei/archive/2012/09/13/2682778.html