码迷,mamicode.com
首页 > 编程语言 > 详细

Python 类 setattr、getattr、hasattr 的使用

时间:2015-06-29 19:28:50      阅读:129      评论:0      收藏:0      [点我收藏+]

标签:

#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 的使用

标签:

原文地址:http://www.cnblogs.com/blogofwyl/p/4607757.html

(0)
(0)
   
举报
评论 一句话评论(0
登录后才能评论!
© 2014 mamicode.com 版权所有  联系我们:gaon5@hotmail.com
迷上了代码!