python 面向对象 __dict__,,打印 类或对象中的所
python 面向对象 __dict__,,打印 类或对象中的所
打印 类或对象中的所有成员
类的构造函数属性 属于对象;类中的公有属性和方法等属于类
打印信息
class schoolMember(object): ‘‘‘学校成员分类‘‘‘ member = 0 def __init__(self, name, age, sex): self.name = name self.age = age self.sex = sex self.enroll() # 实例化时候 自动注册 def enroll(self): ‘‘‘注册‘‘‘ print("just enrolled a new school member [%s]" % self.name) schoolMember.member += 1 def tell(self): print("-------%s info-------" % self.name) def __del__(self): # 析构函数 print("开除了 [%s]" % self.name) schoolMember.member -= 1class Teather(schoolMember): ‘‘‘教师类‘‘‘ def __init__(self, name, age, sex, salary, course): schoolMember.__init__(self, name, age, sex) self.salary = salary self.course = course def teaching(self): print("tearcher [%s] is teaching [%s]" % (self.name, self.course)) def tell(self): print(‘‘‘ ----------info:%s---------- name:%s age:%s salary:%s ‘‘‘ % (self.name,self.name,self.age,self.salary))class Student(schoolMember): ‘‘‘学生类‘‘‘ def __init__(self, name, age, sex, course, tuition): schoolMember.__init__(self, name, age, sex) self.course = course self.tuition = tuition self.amount = 0 def pay_tuition(self, amount): print("student [%s] has just paied [%s]" % (self.name, amount)) self.amount += amountt1 = Teather("mike", 28, "F*M", 10000, "python")s1 = Student("jianlin", 21, "N/A","pythons15", 100000)s2 = Student("mayun", 12, "M", "pythons15", 210000)print(schoolMember.member)print("-"*40)t1.tell()print("-"*40)del s1print(schoolMember.member)del s2print(schoolMember.member)del t1print(schoolMember.member)‘‘‘just enrolled a new school member [mike]just enrolled a new school member [jianlin]just enrolled a new school member [mayun]3---------------------------------------- ----------info:mike---------- name:mike age:28 salary:10000 ----------------------------------------开除了 [jianlin]2开除了 [mayun]1开除了 [mike]0‘‘‘
用__dict__ 以字典方式打印所有属性
class schoolMember(object): ‘‘‘学校成员分类‘‘‘ member = 0 def __init__(self, name, age, sex): self.name = name self.age = age self.sex = sex self.enroll() # 实例化时候 自动注册 def enroll(self): ‘‘‘注册‘‘‘ print("just enrolled a new school member [%s]" % self.name) schoolMember.member += 1 def tell(self): print("-------%s info-------" % self.name) def __del__(self): # 析构函数 print("开除了 [%s]" % self.name) schoolMember.member -= 1class Teather(schoolMember): ‘‘‘教师类‘‘‘ def __init__(self, name, age, sex, salary, course): schoolMember.__init__(self, name, age, sex) self.salary = salary self.course = course def teaching(self): print("tearcher [%s] is teaching [%s]" % (self.name, self.course))class Student(schoolMember): ‘‘‘学生类‘‘‘ def __init__(self, name, age, sex, course, tuition): schoolMember.__init__(self, name, age, sex) self.course = course self.tuition = tuition self.amount = 0 def pay_tuition(self, amount): print("student [%s] has just paied [%s]" % (self.name, amount)) self.amount += amountt1 = Teather("mike", 28, "F*M", 10000, "python")s1 = Student("jianlin", 21, "N/A","pythons15", 100000)s2 = Student("mayun", 12, "M", "pythons15", 210000)print(schoolMember.member)print("-"*40)print(t1.__dict__) # # 以字典方式打印所有属性print("-"*40)del s1print(schoolMember.member)del s2print(schoolMember.member)del t1print(schoolMember.member)‘‘‘just enrolled a new school member [mike]just enrolled a new school member [jianlin]just enrolled a new school member [mayun]3----------------------------------------{‘name‘: ‘mike‘, ‘age‘: 28, ‘sex‘: ‘F*M‘, ‘salary‘: 10000, ‘course‘: ‘python‘}----------------------------------------开除了 [jianlin]2开除了 [mayun]1开除了 [mike]0‘‘‘
class schoolMember(object): ‘‘‘学校成员分类‘‘‘ member = 0 def __init__(self, name, age, sex): self.name = name self.age = age self.sex = sex self.enroll() # 实例化时候 自动注册 def enroll(self): ‘‘‘注册‘‘‘ print("just enrolled a new school member [%s]" % self.name) schoolMember.member += 1 def tell(self): print("%s info".center(40, "-") % self.name) for i in self.__dict__: # 循环字典 t1.__dict__ print("\t", i, self.__dict__[i]) print("End".center(40, "-")+"\n") def __del__(self): # 析构函数 print("开除了 [%s]" % self.name) schoolMember.member -= 1class Teather(schoolMember): ‘‘‘教师类‘‘‘ def __init__(self, name, age, sex, salary, course): schoolMember.__init__(self, name, age, sex) self.salary = salary self.course = course def teaching(self): print("tearcher [%s] is teaching [%s]" % (self.name, self.course))class Student(schoolMember): ‘‘‘学生类‘‘‘ def __init__(self, name, age, sex, course, tuition): schoolMember.__init__(self, name, age, sex) self.course = course self.tuition = tuition self.amount = 0 def pay_tuition(self, amount): print("student [%s] has just paied [%s]" % (self.name, amount)) self.amount += amountt1 = Teather("mike", 28, "F*M", 10000, "python")s1 = Student("jianlin", 21, "N/A","pythons15", 100000)s2 = Student("mayun", 12, "M", "pythons15", 210000)print(schoolMember.member)t1.tell()s1.tell()s2.tell()del s1print(schoolMember.member)del s2print(schoolMember.member)del t1print(schoolMember.member)‘‘‘just enrolled a new school member [mike]just enrolled a new school member [jianlin]just enrolled a new school member [mayun]3----------------mike info----------------- name mike age 28 sex F*M salary 10000 course python------------------End-----------------------------------jianlin info----------------- name jianlin age 21 sex N/A course pythons15 tuition 100000 amount 0------------------End-----------------------------------mayun info----------------- name mayun age 12 sex M course pythons15 tuition 210000 amount 0------------------End-------------------开除了 [jianlin]2开除了 [mayun]1开除了 [mike]0‘‘‘
以字典形式打印这个类所有属性,方法
获取类的成员,即:公有属性、方法等
#! /usr/bin/env python# -*- coding: utf-8 -*-# __author__ = "minger"# Date: 2018/2/27class dog(object): def __init__(self,name): self.name = name def eat(self): print("%s is eating %s" % (self.name,‘ss‘)) def talk(self): print("%s is taking" % self.name)print(dog.__dict__) # 以字典形式打印这个类所有属性,方法# {‘__module__‘: ‘__main__‘, ‘__init__‘: <function dog.__init__ at 0x000000000283F7B8>, ‘eat‘: <function dog.eat at 0x000000000283F840>, ‘talk‘: <function dog.talk at 0x000000000283F8C8>, ‘__dict__‘: <attribute ‘__dict__‘ of ‘dog‘ objects>, ‘__weakref__‘: <attribute ‘__weakref__‘ of ‘dog‘ objects>, ‘__doc__‘: None}
只打印实例变量属性,不包括类属性
class dog(object): def __init__(self,name): self.name = name def eat(self): print("%s is eating %s" % (self.name,‘ss‘)) def talk(self): print("%s is taking" % self.name)# print(dog.__dict__) # 以字典形式打印这个类所有属性,方法d = dog("mike")print(d.__dict__) # 只打印实例变量属性,不包括类属性# {‘name‘: ‘mike‘}
python 面向对象 __dict__
评论关闭