首页
学习
活动
专区
工具
TVP
发布
精选内容/技术社群/优惠产品,尽在小程序
立即前往

【python进阶】-dict和object

1

dict和object可以说是python的精华

首先,dict就是一系列的key,value对,而,object说白了就是dict的升级版:

就会生成object的所有属性则生成object的所有字段

不懂区别的请自觉恶补高级语言对象部分的通用知识

可以更新object内容 综上所述,其实dict,object可以看成不同的字典

2

python的property

高级语言都可以有一个特殊的字段,在进行赋值取值的时候执行操作。

python实现为property

具体实现有两种:

@propertydef [property name](self): // to do something before return return some@[propety name].setterdef [propety name](self, value): // to do something before save // save value to some variant

[property getter](self): // to do something before return return some[propety setter].setter(self, value): // to do something before save // save value to some variant[property] = (fget=[property getter], fset=[propety setter])

最终导致,想通过dict批量修改object的时候,property属性会被污染,而导致不起作用。

3

完美替代__dict__

#coding:utf-8from Tkinter import * #引用Tk模块import redef props(obj): pr = {} for key in dir(obj): f = "get_" + key if hasattr(obj, f) and callable(getattr(obj, f)): f = getattr(obj, f) pr[key] = f() continue value = getattr(obj, key) if not key.startswith('__') and not callable(value) and not key.startswith('_'): pr[key] = value return pr

4

批量设值和取值

def dictcopy(dest, source, keys = None): if type(keys) != list: for key in source: dest[key] = source[key] else: for key in keys: if key in source: dest[key] = source[key] else: dest[key] = None return dest def objectsub(source, keys, values =None, valids = None, todict = True): if type(source)!=dict and isinstance(source, object): source = props(source) sub = dictsub(source, keys, values, valids) if todict: return sub else: r = object() r.__dict__.update(r) return rdef objectset(obj, source, keys = None): if type(obj) == dict: buf = object() buf.__dict__.update(obj) obj = buf elif not isinstance(obj, object): return obj if type(source) != dict and isinstance(source, object): source = props(source) pr = {} pr = dictcopy(pr, source, keys) dpr = dir(obj) for key in pr: f = "set_" + key if hasattr(obj, f) and callable(getattr(obj, f)): f = getattr(obj, f) f(pr[key]) else: setattr(obj, key, pr[key]) return obj

5

必要的dict的进阶函数

def dictsub(d, keys, values = None, valids = None): if type(d) != dict: d = {} if type(values) == NoneType: values = {} if type(valids) == NoneType: valids = {} r = {} f = None for key in keys: f = None if (key in valids) and callable(valids[key]): f = valids[key] if (key in d) and ((not f) or f(d[key])): r[key] = d[key] elif key in values and ((not f) or f(values[key])): r[key] = values[key] else: r[key] = None return rdef OptFit(df = None): if type(df) != dict: return def fn(d): if type(d) != dict : return r = {} for k in df: if k in d: if callable(df[k]): r[k] = df[k](d[k], k, d[k]) else: r[k] = d[k] else: if callable(df[k]): r[k] = df[k]("", "", "") else: r[k] = df[k] return r return fn

  • 发表于:
  • 原文链接https://kuaibao.qq.com/s/20190201G0G58H00?refer=cp_1026
  • 腾讯「腾讯云开发者社区」是腾讯内容开放平台帐号(企鹅号)传播渠道之一,根据《腾讯内容开放平台服务协议》转载发布内容。
  • 如有侵权,请联系 cloudcommunity@tencent.com 删除。

扫码

添加站长 进交流群

领取专属 10元无门槛券

私享最新 技术干货

扫码加入开发者社群
领券