简体   繁体   中英

pass a function parameter as an argument to another function in python

i have two functions in python

class JENKINS_JOB_INFO():
    def __init__(self):
        parser = argparse.ArgumentParser(description='xxxx. e.g., script.py -j jenkins_url -u user -a api')
        parser.add_argument('-j', '--address', dest='address', default="", required=True, action="store")
        parser.add_argument('-u', '--user', dest='user', default="xxxx", required=True, action="store")
        parser.add_argument('-t', '--api_token', dest='api_token', required=True, action="store")
        parsers = parser.parse_args()

        self.base_url = parsers.address.strip()
        self.user = parsers.user.strip()
        self.api_token = parsers.api_token.strip()
    def main(self):
        logger.info("call the function")
        self.get_jobs_state()
    def get_jobs_state(self):
        get_jenkins_json_data(params)

   def get_jenkins_json_data(self, params, base_url):
        url = urljoin(self.base_url, str(params.params))
        r = requests.get(url, auth=HTTPBasicAuth(self.user, self.api_token), verify=False)

i have a parameter params defined in my function get_jobs_state and i want to pass this param to my other function get_jenkins_json_data so that the complete url inside function get_jenkins_json_data joins to https:<jenkins>/api/json?pretty=true&tree=jobs[name,color]

But when i run my code the url is not correct and the value of params inside the function is <__main__.CLASS_NAME instance at 0x7f9adf4c0ab8>

here base_url is a parameter that i am passing to my script.

How can i get rid of this error?

Just write params.params instead of params .

The way you do it is extremely confusing because in get_jenkins_josn_data , self will be the params and params will be the base_url . I would advise you no to do that in the future. If you want to send some parameters to the function, send the minimal amount of information that the function needs. Here, for example, you could have sent self.params instead of the whole self . This way you wouldn't encounter this error and the code would be much more readable.

I suggest you to rewrite this function this way.

In your function get_jobs_state you are passing in self as the argument for the params argument to the get_jenkins_json_data , and self in this case is an instance of a class.

Try something like this:

class Jenkins:

def __init__(self, domain):

    self.user = "user"
    self.api_token = "api_token"
    self.base_domain = domain

def get_jobs_state(self):
    query = "api/json?pretty=true&tree=jobs[name,color]"
    return self.get_jenkins_json_data(query)

def get_jenkins_json_data(self, query):
    url = urljoin(self.base_domain, str(query))
    r = requests.get(url, auth=HTTPBasicAuth(self.user, self.api_token), verify=False)
    return r

So your solution is a bit confusing. You shouldn't pass the self to the get_jenkins_json_data method. The python will do that for you automatically. You should check out the data model for how instance methods work. I would refactor your code like this:

def get_jobs_state(self):
    params = "api/json?pretty=true&tree=jobs[name,color]"
    self.get_jenkins_json_data(params)

def get_jenkins_json_data(self, params):
    url = urljoin(self.base_url, params)
    r = requests.get(url, auth=HTTPBasicAuth(self.user, self.api_token), verify=False)
...

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM