# -*- coding: utf-8 -*- #python 27 #xiaodeng #http://www.360doc.com/content/15/0413/19/12067640_462966543.shtml #类的专有方法(__getattr__和__setattr__.__delattr__) #__setattr__:通过该方法,给对象添加或修改指定的属性 class Test(): def __init__(self,name,age): self.name=name se…
访问顺序: 实例的__getattribute__().Descriptor的__get__().实例的__dict__.只读Descriptor的__get__().实例的__getattr__(): 实例的__setattr__().Descriptor的__set__().实例的__dict__: 实例的__delattr__().Descriptor的__delete__().实例的__dict__.…
__getattr__:     属性查找失败后,解释器会调用 __getattr__ 方法. class TmpTest: def __init__(self): self.tmp = 'tmp123' def __getattr__(self, item): raise AttributeError('{} object has no attribute {}'.format(type(self), item)) a=TmpTest() print(a.tmp) 结果: tmp123 pri…
1. ConfigParser format.conf [DEFAULT] conn_str = %(dbn)s://%(user)s:%(pw)s@%(host)s:%(port)s/%(db)s dbn = mysql user = root host = localhost port = 3306 [db1] user = aaa pw = ppp db = example [db2] host = 172.16.88.1 pw = www db = example readformati…
python之 __getattr__.__getattr__.__getitem__.__setitem__ 使用 __getattr__内置使用点号获取实例属性属性如 s.name,自调用__getattr__ __setattr__设置类实例属性 如s.name='tom',自调用__setattr__ __getitem__ 使用[]获取实例属性 如s['name'],自调用__getitem__ __setitem__ 使用[]设置实例属性如 s['name'] = 'tom' ,自调…
一. 引言 在<第7.23节 Python使用property函数定义属性简化属性访问的代码实现>和<第7.26节 Python中的@property装饰器定义属性访问方法getter.setter.deleter 详解>中介绍了两种设置属性访问方法,通过设置可以在相关属性访问时调用对应的方法执行访问,支持属性简单访问(如对象名.属性名.赋值语句).或者为了控制访问逻辑使用的.那么property函数其中的fget.@property装饰器的getter之间的关系是怎样的呢?下面我…
new方法和单例.定制访问函数.装饰器 上节课作业解答 # 通过多重继承方法,分别定义出动物,人类,和和荷兰人三种类 class Animal(object): def __init__(self, name): self.name = name ​ def eat(self): print('%s正在吃东西' % self.name) ​ def breath(self): print('%s正在呼吸' % self.name) ​ ​ class Person(Animal): def __…
主要讲类的内部方法 __setattr__  __getattr_  __delattr__  hasattr  __getattribute__  __getitem__(),__setitem__(), __delitem__() 主程序如下: class Foo: def _inif(self,pname): self.pname = pname def func(): print('i'm func') def __getattr__(self, item): print('提示:属性[…
目录 一.__setattr__ 二.__delattr__ 三. __getattr__ class Foo: x = 1 def __init__(self, y): self.y = y def __getattr__(self, item): print('----> from getattr:你找的属性不存在') def __setattr__(self, key, value): print('----> from setattr') # self.key = value # 这就…
一.可变参数 定义函数时,有时候我们不确定调用的时候会传递多少个参数(不传参也可以).此时,可用包裹(packing)位置参数(*args),或者包裹关键字参数(**kwargs),来进行参数传递,会显得非常方便. 1.包裹位置传递 def send_sms(*args): # 可变参数,参数组 print('phones',args) def say(word): print(word) say(word='nihao') send_sms(110,138,119) say('nihao')…