python之函数用法__setattr__
# -*- coding: utf-8 -*-
#python 27
#xiaodeng
#python之函数用法__setattr__
#http://www.cnblogs.com/hongfei/p/3858256.html #用__setattr__函数重构方法
class Fruit():
def __init__(self,color,price):
self.__color = color
self.__price = price def __setattr__(self,name,value):#重构方法__setattr__方法,没有该方法会默认存在该方法
self.__dict__[name] = str('xiaodeng,')+str(value) #为了看出区别,特意在__setattr__方法中加了str('xiaodeng') if __name__ == "__main__":
fruit = Fruit("blue", 10)
print fruit #__main__.Fruit instance
print fruit.__dict__ #{'_Fruit__price': 'xiaodeng,10', '_Fruit__color': 'xiaodeng,blue'}
print fruit.__dict__.get("_Fruit__price") #xiaodeng,10 #给_Fruit__price设置新的属性
fruit.__dict__["_Fruit__color"] = 'red'
print fruit.__dict__.get("_Fruit__color") #red #增加新的属性和属性值
fruit.__dict__['name']='xiaodeng'
print fruit.__dict__ #{'_Fruit__price': 'xiaodeng,10', 'name': 'xiaodeng', '_Fruit__color': 'red'}
python之函数用法__setattr__的更多相关文章
- python之函数用法setattr(),了解即可
# -*- coding: utf-8 -*- #python 27 #xiaodeng #python之函数用法setattr(),了解即可 #http://www.cnblogs.com/hong ...
- Python回调函数用法实例详解
本文实例讲述了Python回调函数用法.分享给大家供大家参考.具体分析如下: 一.百度百科上对回调函数的解释: 回调函数就是一个通过函数指针调用的函数.如果你把函数的指针(地址)作为参数传递给另一个函 ...
- python之函数用法setdefault()
# -*- coding: utf-8 -*- #python 27 #xiaodeng #python之函数用法setdefault() #D.get(k,d) #说明:k在D中,则返回 D[K], ...
- python之函数用法fromkeys()
# -*- coding: utf-8 -*- #python 27 #xiaodeng #python之函数用法fromkeys() #fromkeys() #说明:用于创建一个新字典,以序列seq ...
- python之函数用法get()
# -*- coding: utf-8 -*- #python 27 #xiaodeng #python之函数用法get() #http://www.runoob.com/python/att-dic ...
- python之函数用法capitalize()
# -*- coding: utf-8 -*- #python 27 #xiaodeng #python之函数用法capitalize() #capitalize() #说明:将字符串的第一个字母变成 ...
- python之函数用法isupper()
# -*- coding: utf-8 -*- #python 27 #xiaodeng #python之函数用法isupper() #http://www.runoob.com/python/att ...
- python之函数用法islower()
# -*- coding: utf-8 -*- #python 27 #xiaodeng #python之函数用法islower() #http://www.runoob.com/python/att ...
- python之函数用法startswith()
# -*- coding: utf-8 -*- #python 27 #xiaodeng #python之函数用法startswith() #http://www.runoob.com/python/ ...
随机推荐
- .NET:CLR via C#:Runtime Serialization
Making a Type Serializable The SerializableAttribute custom attribute may be applied to reference ty ...
- [集合框架] List 实现
List 实现分为通用 List 实现和特殊用途的 List 实现. 通用 List 实现 有两个通用的 List 实现 —— ArrayList 和 LinkedList.大多数时候,你可能会使用 ...
- FFMPEG视音频编解码零基础学习方法 【荐】
在CSDN上的这一段日子,接触到了很多同行业的人,尤其是使用FFMPEG进行视音频 编解码的人,有的已经是有多年经验的“大神”,有的是刚开始学习的初学者.在和大家探讨的过程中,我忽然发现了一个问题:在 ...
- iOS:NSDate的主要几种时间形式
NSDate:时间的获取和操作 1.获取当前时间 //获取当前日期 NSDate *date = sender.date; NSLog(@"%@",date); 2.将date转换 ...
- GPU/CUDA程序初体验 向量加法
现在主要的并行计算设备有两种发展趋势: (1)多核CPU. 双核,四核,八核,...,72核,...,可以使用OpenMP编译处理方案,就是指导编译器编译为多核并行执行. (2)多线程设备(GP)GP ...
- Android UI-仿微信底部导航栏布局
现在App基本的标配除了侧滑菜单,还有一个就是底部导航栏,常见的聊天工具QQ,微信,购物App都有底部导航栏,用户可以随便切换看不同的内容,说是情怀也好,用户体验也罢.我们开发的主要的还是讲的是如何如 ...
- 3 Sum leetcode java
题目: Given an array S of n integers, are there elements a, b, c in S such that a + b + c = 0? Find al ...
- 从javascript读取cookies说开去:谈谈网页的本地化存储
学习要点:1.cookies 2.cookies 局限性 3.其他存储 随着 Web 越来越复杂,开发者急切的需要能够本地化存储的脚本功能.这个时候,第一个出现的方案:cookie 诞生了.cooki ...
- IOS UITableView索引排序功能
UITbableView分组展示信息时,有时在右侧会带索引,右侧的索引一般为分组的首字母,比如城市列表的展示.当点击右侧索引的字母,列表会快速跳到索引对应的分组,方便我们快速查找.下面,就介绍一下索引 ...
- CommonCode升级:把不常用的Sqlite独立出去
CommonCode大概一年多没有更新了,今天碰到一件闹心的事情,结果用一行代码解决了京东购物车信息提取工具.一不小心,把一贯以来的一个念头又给惹起来了:就是把程序做成又给独立的exe,不要这么多dl ...