#coding=utf-8
class Employee:
'所有员工的基类'
empCount = 0
def __init__(self, name, salary):
self.name = name
self.salary = salary
Employee.empCount += 1
def displayCount(self):
print ("Total Employee %d" % Employee.empCount)
def displayEmployee(self):
print ("Name : ", self.name, "\nSalary: ", self.salary)
Xiaoxiao = Employee('Xiaoxiao', 2000)
setattr(Xiaoxiao, 'age', 21)
Tiny = Employee("Tiny", 5000)
#setattr(Tiny, 'age', 23)
print ("实例 Employee 类的第一个对象 Xiaoxiao ");
print ('Xiaoxiao 是否存在age属性:',hasattr(Xiaoxiao,'age'))
Xiaoxiao.displayEmployee();
print("Age: ",getattr(Xiaoxiao,'age', 'not find'));
print ("\n")
print ("实例 Employee 类的第二个对象 Tiny")
print ('Tiny 是否存在age属性:',hasattr(Tiny,'age'))
Tiny.displayEmployee()
print("Age: ",getattr(Tiny,'age', 'not find'));
print ("\n")
print ("Total Employee number: %d" % Employee.empCount)
print ("\n")

Python 类 setattr、getattr、hasattr 的使用的更多相关文章

  1. python之setattr,getattr,hasattr

    可以使用setattr(), getattr(), hasattr()动态对实例进行操作. 相当于Java中的反射机制, 或者更确切地, 像JavaScript中属性操作. 具体属性: __dict_ ...

  2. Python的getattr(),setattr(),delattr(),hasattr()及类内建__getattr__应用

    @Python的getattr(),setattr(),delattr(),hasattr() 先转一篇博文,参考.最后再给出一个例子 getattr()函数是Python自省的核心函数,具体使用大体 ...

  3. getattr、setattr、hasattr

    写一个演示类 class test(): title="验证getattr.setattr.hasattr方法" def run(self): return "run方法 ...

  4. python 内建函数setattr() getattr()

    python 内建函数setattr() getattr() setattr(object,name,value): 作用:设置object的名称为name(type:string)的属性的属性值为v ...

  5. Python自省 type(),dir(),getattr(),hasattr(),isinstance().

    Python自省 这个也是python彪悍的特性. 自省就是面向对象的语言所写的程序在运行时,所能知道对象的类型.简单一句就是运行时能够获得对象的类型.比如type(),dir(),getattr() ...

  6. python自省函数getattr的用法

    getattr是python里的一个内建函数 getattr()这个方法最主要的作用是实现反射机制.也就是说可以通过字符串获取方法实例.这样,你就可以把一个类可能要调用的方法放在配置文件里,在需要的时 ...

  7. python 类知识点总结

    python 类知识点总结 面向对象思想: 1.设计的时候,一定要明确应用场景 2.由对象分析定义类的时候,找不到共同特征和技能不用强求 1.简述类.对象.实例化.实例这些名词的含义: 类:从一组对象 ...

  8. python学习之 getattr vs __get__ vs __getattr __ vs __getattribute__ vs __getitem__

    1. getattr.setattr.hasattr getattr比较常用,与setattr和hasattr一起出现,他们也是最容易理解的,下面是他的用法: class Profile(): nam ...

  9. python类的相关知识第二部分

    类的继承.多态.封装 一.类的继承 1.应用场景: 类大部分功能相同,大类包含小类的情况 例如: 动物类 共性:都要吃喝拉撒.都有头有脚 特性: 猫类.走了很轻,叫声特别,喜欢白天睡觉 狗类.的叫声很 ...

随机推荐

  1. 【转】文件中有10G个整数,乱序排列,要求找出中位数

    题目:在一个文件中有 10G 个整数,乱序排列,要求找出中位数.内存限制为 2G.只写出思路即可(内存限制为 2G的意思就是,可以使用2G的空间来运行程序,而不考虑这台机器上的其他软件的占用内存). ...

  2. Linux 下mysql忘记root密码解决方法

    忘记root密码怎么办:1.关闭数据库2.使用-->mysqld_safe --skip-grant-tables &--<启动数据库3.使用空密码进入数据库(mysql命令后直接 ...

  3. Linux下pcapy的安装问题

    在安装pcapy包的时候 cd pcapy-0.10.8/ python setup.py install 报错 error trying to exec ‘cc1plus’: execvp: No ...

  4. Eclipse SVN插件账号、密码修改

    操作系统:win7 svn插件:Window -> Preferences -> Team -> SVN 修改方式: 1,删除C:\Users\用户名\AppData\Roaming ...

  5. 判断是否为闰年(bool)

    bool为布尔型,只有一个字节,取值false和true #include<iostream>using namespace std;int main(){ int year; bool ...

  6. jQuery 常用代码集锦

    1. 选择或者不选页面上全部复选框 var tog = false; // or true if they are checked on load $('a').click(function() { ...

  7. (poj)3414 Pots (输出路径的广搜)

    Description You are given two pots, having the volume of A and B liters respectively. The following ...

  8. (转) UIALertView的基本用法与UIAlertViewDelegate对对话框的事件处理方法

    首先,视图控制器必须得实现协议UIAlertViewDelegate中的方法,并指定delegate为self,才能使弹出的Alert窗口响应点击事件. 具体代码如下: #import <UIK ...

  9. 用Session实现验证码

    新建一个 ashx 一般处理程序 如: YZM.ashx继承接口 IRequiresSessionState //在一般处理程序里面继承 HttpContext context 为请求上下文,包含此次 ...

  10. 网络安全设备Bypass功能介绍及分析

    from:http://netsecurity.51cto.com/art/200910/159948.htm 网络安全平台厂商往往需要用到一项比较特殊的技术,那就是Bypass,那么到底什么是Byp ...