0

Sorry for the noob question about classes. I'm trying to assign a soap client to a variable inside a class function and then access that variable in other class functions. I don't have any arguments to pass to the setup_client() function.

In the following example code, how do I make self.client accessible outside setup_client() so that I can use it in use_client(), and ditto making self.response available outside use_client()

class soap_call(self):
    def __init__(self):
        # What goes here?
        self.client = # what?
        self.response = # what?

    def setup_client(self):
        credentials = {'username': 'stuff', 'password': 'stuff'}
        url = 'stuff'
        t = HttpAuthenticated(**credentials)
        self.client = suds.client.Client(url, transport=t)

    def use_client(self):
        self.response = self.client.service.whatever
        print self.response

I quickly realized that if I add an optional client argument (self, client=None) to the class definition and include self.client = client, then I get a None type error when trying to use it in my functions.

I realize I just have a lack of understanding of classes. I've done some general reading up on classes but haven't come across any specific examples that describe what I'm dealing with.

2
  • Is the use of self as the base class for soap_call a typo? Commented May 16, 2015 at 18:45
  • @chepner No, it's jut my general lack of understanding, and that my IDE automatically added (self) to soap_call. Commented May 16, 2015 at 18:49

2 Answers 2

2

I'd go with None in both cases because logically speaking, none of them exists at the time of object instantiation. It also allows you to do some sanity checking of your logic, e.g.

class SoapCall(object):
    def __init__(self):
        self.client = None
        self.response = None

    def setup_client(self):
        credentials = {'username': 'stuff', 'password': 'stuff'}
        url = 'stuff'
        t = HttpAuthenticated(**credentials)
        if self.client is None:
            self.client = suds.client.Client(url, transport=t)

    def use_client(self):
        if self.client is None:
            self.client = self.setup_client()

        self.response = self.client.service.whatever
        print self.response
Sign up to request clarification or add additional context in comments.

1 Comment

Incidentally, I was calling my functions incorrectly, but thank you for this. It was very helpful.
0

It's fine to leave the client unspecified when you first create the instance, but then you need to be sure to call setup_client before you call use_client.

2 Comments

I tried that, which is the problem. self.client is unavailable to use_client in the current code.
Yes, which is why I said you have to call setup_client before you call use_client. What does it mean to call use_client before you have client defined?

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.