Network Security Internet Technology Development Database Servers Mobile Phone Android Software Apple Software Computer Software News IT Information

In addition to Weibo, there is also WeChat

Please pay attention

WeChat public account

Shulou

Python knowledge points: reflective getattr () implementation imports modules and executes functions in the form of strings

2025-02-28 Update From: SLTechnology News&Howtos shulou NAV: SLTechnology News&Howtos > Database >

Share

Shulou(Shulou.com)06/01 Report--

#-*-coding: utf-8-*-# knowledge point: reflection # imports the module in the form of a string, which is reflection # and executes the function in the form of a string # first look at the simple small particle temp = 'mysql' # this string is the name of the module (that is, mysql.py or sqlserver.py The suffix name is not required) func = 'count' # this string is the name of the function (that is, the function count in mysql.py/sqlserver.py) model = _ _ import__ (temp) # import the module in the form of a string # the first method of the function in the execution module print model.count () # the function in the execution module the second method Function = getattr (model Func) # execute the function print Function () as a string below simulate a database connection of small particles # prepare the following two files' mysql.py file to simulate connection to the mysql database Normal connection returns "- *-coding: utf-8-*-def count (): return 1sqlserver.py file simulates connecting to sqlserver database, and normal connection returns"-*-coding: utf-8-*-def count (): return 2 subscription. # Note: mysql.py and sqlserver.py files can be defined by themselves. Here is the statement def db_count (db_name) that is not actually connected to the database: temp = db_name # module name (that is, mysql or sqlserver) func = 'count' # function (that is, function count in mysql.py/sqlserver.py) model = _ _ import__ (temp) Function = getattr (model) Func) # execute the function return Function () as a string # currently simulates the default connection to the sqlserver database status = db_count ('sqlserver') # check the status of the connection sqlserver (normal state: 2) If you cannot connect, switch to mysqlif status! = 2: status = db_count ('mysql') print' has been switched to mysql, connection ID:%d'% statuselse: print 'sqlserver connection is normal, connection ID:%d'% status# test action: # 1, the count return value in sqlserver.py can be modified to 3

Welcome to subscribe "Shulou Technology Information " to get latest news, interesting things and hot topics in the IT industry, and controls the hottest and latest Internet news, technology news and IT industry trends.

Views: 0

*The comments in the above article only represent the author's personal views and do not represent the views and positions of this website. If you have more insights, please feel free to contribute and share.

Share To

Database

Wechat

© 2024 shulou.com SLNews company. All rights reserved.

12
Report