python - 在另一个脚本中使用类中的变量
问题描述
基本上,我需要在脚本中存储一些变量并在另一个脚本中使用它们:
存储变量.py
class Setup():
def setup():
a = 'X'
if a == 'X':
#Credentials for X
database = 'a'
user = 'b'
password = 'c'
host = 'd'
port = 4
elif a == 'Y':
#Credentials for Y
database = 'x'
user = 'y'
password = 'z'
host = 'w'
port = 7
然后在我的第二个脚本中使用基于“if”语句的特定变量,如下所示:
连接.py
class Connection:
def __init__(self, host, database, port, user, password):
self.conn = psycopg2.connect(
database=database,
user=user,
password=password,
host=host,
port=port
)
解决方案
您需要为第一个类(设置)创建一个实例,然后使用变量。
s = setup()
c = connection (s.host,s.username...)
推荐阅读
- here-api - HERE-maps 自动完成的 CORS 问题
- android - 从外部应用程序下载并打开 PDF 文件
- eclipse - 类 GoogleMapView 和 javaFX
- xamarin - Xamarin Forms / MvvmCross 架构与 ICommand.Execute(Object) 模式?
- reactjs - 如何获得相机预览的高度?
- amazon-dynamodb - AWS AppSync GraphQL 按字段值查询记录
- java - 按下按钮后活动关闭
- java - 使用迭代器 - java.util.ConcurrentModificationException
- javascript - 如何在窗口调整大小时重新定位对象
- hashmap - 为什么选择HashMap
是无法访问的代码。Java 8 中的 HashMap