对象的__dict__属性
2016-01-23 23:33
459 查看
#-*-coding:utf-8-*- ''' Created on 2016年1月23日 @author: Zroad ''' class Person(object): def __init__(self,sex,age): self.sex = sex self.age = age def set_height(self,height): self.height = height def get_height(self): return self.height class Animal(object): __slots__ = ('name','age') def __init__(self,name,age): self.name = name self.age = age p = Person('男', '20') p.set_height(165) a = Animal('horse',1) """ __dict__: 1、记录对象属性名、属性值的字典,本例中输出:(相当于java中的成员变量) {'age': '20', 'height': 165, 'sex': '\xc4\xd0'} 2、不是所有的对象都有__dict__属性:设置__slots__属性的对象没有,一些内置的对象没有 3、内置的dir(object)可以输出对象所有属性,包括设置了__slots属性的 """ print p.__dict__ """ dir函数返回对象所有的属性,包括内置的、自定义的,返回值为列表,只是列出属性名称、方法名称等 """ print dir(p) """ 运行报错: AttributeError: 'Animal' object has no attribute '__dict__' """ #print a.__dict__
相关文章推荐
- Python动态类型的学习---引用的理解
- Python3写爬虫(四)多线程实现数据爬取
- 垃圾邮件过滤器 python简单实现
- 下载并遍历 names.txt 文件,输出长度最长的回文人名。
- install and upgrade scrapy
- Scrapy的架构介绍
- Centos6 编译安装Python
- 使用Python生成Excel格式的图片
- 让Python文件也可以当bat文件运行
- [Python]推算数独
- Python中zip()函数用法举例
- Python中map()函数浅析
- Python将excel导入到mysql中
- Python在CAM软件Genesis2000中的应用
- 使用Shiboken为C++和Qt库创建Python绑定
- FREEBASIC 编译可被python调用的dll函数示例
- Python 七步捉虫法