python之反射

  1 # # 什么是反射?   *****
  2 # # 用字符串数据类型的变量名来访问这个变量的值
  3 # # 类
  4 # # 反射的方法 : getattr hasattr setattr delattr
  5 # # 类    静态属性 类方法 静态方法
  6 # # 命名空间.xxx == getsttr(命名空间,'xxx')
  7 # class Student:
  8 #     ROLE = 'STU'  # 静态属性
  9 #
 10 #     @classmethod
 11 #     def check_course(cls):  # 类方法
 12 #         print('查看课程了')
 13 #
 14 #     @staticmethod
 15 #     def login():  # 静态方法 不需要传入参数即可调用
 16 #         print('登入')
 17 #
 18 #
 19 # # 反射查看属性
 20 # print(getattr(Student, 'ROLE'))
 21 # # 反射调用方法
 22 # getattr(Student, 'check_course')()
 23 # # 反射调用静态方法
 24 # getattr(Student, 'login')()
 25 #
 26 #
 27 # # str = input('>>>')
 28 # # if hasattr(Student, str):  # 表示输入的字符串是否在Student中是否存在,如果存在返回True
 29 # #     getattr(Student, str)()
 30 #
 31 #
 32 # # 第一个参数的命名空间中的变量名为第二个参数变量的值
 33 # # 对象 方法和对象属性
 34 # class A:
 35 #     def __init__(self, name):
 36 #         self.name = name
 37 #
 38 #     def func(self):
 39 #         print('in func')
 40 #
 41 #
 42 # a = A('yuxin')
 43 # a.func()
 44 # print(a.name)
 45 # print(getattr(a, 'name'))
 46 # getattr(a, 'func')()
 47 #
 48 #
 49 # # 模块
 50 # # import os
 51 # # os.rename('__init__.py', 'init')
 52 # # getattr(os, 'rename')('init', '__init__.py')
 53 # # 反射自己模块中的内容
 54 # def hahaha():
 55 #     print('哈哈哈')
 56 #
 57 #
 58 # def qqing():
 59 #     print('qqing')
 60 #
 61 #
 62 # import sys
 63 #
 64 # # print(sys.modules)
 65 # print(sys.modules['__main__'])
 66 # my_file = sys.modules['__main__']
 67 # getattr(my_file, 'hahaha')()
 68 #
 69 #
 70 # # 反射
 71 # # hasattr getattr
 72 # # 类名.名字
 73 # #           getattr(类名, '名字')
 74 # # 对象名.名字
 75 # #           getattr(对象名, '名字')
 76 # # 模块名.名字
 77 # #           getattr(模块名, '名字')
 78 # #           import 模块
 79 # #            getattr(模块,'名字')
 80 # # 自己文件.名字
 81 # #           import sys
 82 # #           getattr(sys.module['__main__'], '名字')
 83 #
 84 # 选课系统的代码
 85 # login
 86 # 判断身份,并且根据身份实例化
 87 # 根据每个身份对应的类,让用户选择能够做的事情
 88 class Manager:
 89     OPERATE_DIC = [
 90         ('创造学生账号', 'create_student'),
 91         ('创建课程', 'create_course'),
 92         ('查看学生信息', 'check_student_info'),
 93     ]
 94     def __init__(self, name):
 95         self.name = name
 96 
 97     def create_student(self):
 98         print('创建学生账号')
 99 
100     def create_course(self):
101         print('创建课程')
102 
103     def check_student_info(self):
104         print('查看学生信息')
105 
106 
107 class Student:
108     OPERATE_DIC = [
109         ('查看所有课程', 'check_course'),
110         ('选择课程', 'choose_course'),
111         ('查看已选择的课程', 'choosed_course')
112     ]
113     def __init__(self, name):
114         self.name = name
115     def choose_course(self):
116         print('choose_course')
117 
118     def check_course(self):
119         print('check_course')
120 
121     def choosed_course(self):
122         print('查看已经选择的课程')
123 
124 
125 
126 def login():
127     username = input("user:")
128     password = input("pwd: ")
129     with open('userinfo') as f:
130         for line in f:
131             user, pwd, ident = line.strip().split('|')  #  strip()去除两端的\n ,split('|')以|切割
132             if user == username and pwd == password:
133                 print('登入成功')
134                 return username, ident
135             else:
136                 print('账户名或密码错误!!')
137                 exit()
138 
139 import sys
140 def main():
141     usr, id = login()
142     print(f'user:{usr} id:{id}')
143     file = sys.modules['__main__']
144     cls = getattr(file, id)  # getattr(当前文件, 'Manager')
145     obj = cls(usr)
146     operate_dic = cls.OPERATE_DIC
147     while True:
148         for num, i in enumerate(operate_dic, 1):
149             print(num, i[0])
150         choice = int(input('num >>>'))
151         choice_item = operate_dic[choice - 1]
152         getattr(obj, choice_item[1])()
153 
154 
155 main()
156 
157 l = ['a', 'b', 'c']
158 for  num, i in enumerate(l):
159     print( num,i)

 

posted @ 2021-11-09 10:59  余鑫2020  阅读(33)  评论(0)    收藏  举报