python - Process variables from one method to another inside one class -


guys, think how process variables 1 method inside 1 class. example :

class newclas:     def getportalsources(self,portal):             self.connection_source=self.config.get("portal_"+portal,'sources')             self.portal=portal      def getconnection(self,source):             self.source=source             self.connection_string=self.config.get('connection',self.portal+'_'+source+'_'+'connectstring')   ## connection 

until used above. on getconnection used self.portal variable getportalsources method. still little bit unclear me.

just wondering if there other better aproach ? if so, give me tips, or examples.

for example :

def getportalsources(self,portal):         self.connection_source=self.config.get("portal_"+portal,'sources')         self.portal=portal  def getconnection(source):         self.connection_string=self.config.get('connection',getportalsources.portal+'_'+source+'_'+'connectstring')   ## connection 

of course not work, think got idea.

regards

what suggest use constructor, or global variables.

i give example constructor here:

class newclas:     def __init__(self,portal='default_portal',source='default_source'):          self.portal = portal          self.source = source      def getportalsources(self,portal=self.portal):             self.connection_source=self.config.get("portal_"+portal,'sources')       def getconnection(self,source=self.source):             self.connection_string=self.config.get('connection',self.portal+'_'+source+'_'+'connectstring')   ## connection 

so what's happening here is, when create object of class make this:

new_obj = newclas(portal='the_portal',source='the source') 

using portal='the_portal',source='the source' optional if don't provide take default value.

and when : new_obj.getconnection() provide thing.

if : new_obj.getconnection(source='some_other_source') give source.


Comments

Popular posts from this blog

Spring Boot + JPA + Hibernate: Unable to locate persister -

go - Golang: panic: runtime error: invalid memory address or nil pointer dereference using bufio.Scanner -

c - double free or corruption (fasttop) -