Python:如何从两个模块导入具有相同名称的类? [英] Python: How to import, from two modules, Classes that have same names?

查看:532
本文介绍了Python:如何从两个模块导入具有相同名称的类?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在编写一个python程序来在不同的数据库之间进行粒度同步。



我正在使用SQLAlchemy和一个名为sqlautocode的模块进行数据库检查和模式类生产。



有两个DB同步,具有相同的表名,sqlautocode编写的类具有相同的名称。



<我必须导入带有任意前缀的theese Classes,我正在考虑这样的事情:

 来自module_name import * with前缀

否则我应该使用as修饰符导入每个Classes名称,如下所示:

 来自module_name import x as master_x 

第一个带有控制列表和exec / eval复杂代码的HardCode名称提取程序我想对它提出一些建议。



更新:
解决方案是一个sqlautocode选项: - table-prefix = TABLE_PREFIX



谢谢大家

解决方案

只需导入模块,不要试图从中提取名称。无论如何,应该偶尔使用来自X import Y 的

  import module_a 
import module_b

module_a.x
module_b.x


I'm writing a python programm to do granular syncs between different DB.

I'm using SQLAlchemy and a module named sqlautocode for DB inspecting and Schema Classes production.

Having two DB to sync, with same tables name, the Classes written by sqlautocode results with same names.

I have to import theese Classes with arbitrary prefixes, I'm thinking about something like this:

from module_name import * with prefixes

Otherwise I should import every Classes name with an "as" modifier, like this:

from module_name import x as master_x

First of HardCode name extraction procedures with control lists and exec/eval complex code I'd like some suggestions about it.

UPDATE: The solution is a sqlautocode option: --table-prefix=TABLE_PREFIX

thank you all

解决方案

Just import the modules and don't try to pull the names from them. from X import Y should be used sporadically, anyway.

import module_a
import module_b

module_a.x
module_b.x

这篇关于Python:如何从两个模块导入具有相同名称的类?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆