作为初学者,我需要求你一些帮助。 我定义了类Config,它从config.ini文件中获取一些信息并将它们放入变量中。 现在我定义class:Connection,来自类Config的结果基础。我试图做很多事,但最后放弃了。有人可以看看吗?
class Config:
def __init__(self,system):
self.config = configparser.ConfigParser()
self.config.read("config.ini")
self.connection_source=self.config.get(system,'Source')
self.system=system
def getsystemSources(self):
return self.connection_source
def getConnection(self,source):
self.source=source
self.connection_string=self.config.get('CONNECTION',self.system+'_'+source+'_'+'connectstring') ## Connection
self.connection_user=self.config.get('CONNECTION',self.system+'_'+source+'_'+'user') ## Connection user
self.connection_password=self.config.get('CONNECTION',self.system+'_'+source+'_'+'password') ## Connection pass
class Connection(Config):
def __init__ (self):
self.connection_string=Config.connection_string
self.connection_user=Config.connection_user
self.connection_password=Config.connection_user
self.connection_source=Config.connection_source
def conn_function(self):
print (self.connection_string)
print (self.connection_user)
print (self.connection_password)
emp1 = Config('Windows')
value=emp1.getsystemSources()
print (value)
emp2 = Connection() -> how to run it ?
答案 0 :(得分:1)
您只需将配置对象传递到__init__
函数
class Config:
def __init__(self,system):
self.config = configparser.ConfigParser()
self.config.read("config.ini")
self.connection_source=self.config.get(system,'Source')
self.getConnection(self.connection_source)
self.system=system
class Connection(Config):
def __init__ (self, system):
Config.__init__(self, system)
emp1 = Connection('Windows')
emp1.conn_function()