Python Basic Learning 1-reflection

Source: Internet
Author: User

#!/usr/bin/env python#-*-coding:utf-8-*-getattr (), hasattr (), delattr (), SetAttr () #反射: A string-based way to manipulate its members in an object (module) import COMMONSINP = input ("Please enter a page method to enter") if Hasattr (COMMONS,INP): #检查commons中是否有对应名称的操作成员  func=getattr (COMMONS,INP) # Reflection finds the action Member  func () #执行找到的成员else:    print ("No input module exists") #动态导入模块和寻找模块中的方法ips =input ("GetAttr") in Commons Please enter the module and page method to enter ") #以 commons/login Form Input module and method name M,f=ips.split ('/') obj=  __import__ (m) #动态导入模块if hasattr (obj,f): # Verify that the method name is specified in the current import module    func1=getattr (obj,f)    func1 () Else:    print ("No input page or module exists") #动态导入模块和寻找模块中的方法 Import module in folder condition Ips=input ("Please enter module and page method required") #以 commons/account Form Input module and method name M,f=ips.split ('/') obj=  __import__ ("lib.") +m,fromlist=true) #动态导入模块if hasattr (obj,f): #验证当前导入模块中是否有指定方法名的方法    func1=getattr (obj,f)    func1 () Else:    Print ("No input page or module exists")

commons.py

#!/usr/bin/env python#-*-coding:utf-8-*-def Login ():    print ("Enter Login Interface") def Home ():    print ("Go to Main interface") def LogOut () :    print ("Log Out Program")

Lib folder under account.py

#!/usr/bin/env python#-*-coding:utf-8-*-def Test ():    print ("test method")

Python Basic Learning 1-reflection

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.