1

I have a class that takes 3 parameters(2 compulsory and 1 optional). This is the part of my code:

def __init__(self, user, url, env=None):
    """init method for the class."""
    self.user = user
    self.url = url
    if env is None:
        self.env = ""
    else:
        self.env = env

def check_file(self):
    # get the env value from a dictionary
    if self.env == "test":
        self.address = value['key1']
    else:
        self.address = value['key2']

The address variable has 2 values, one for production env and one for test env. If this env variable is passed as p then the value of self.address should be production url address and if the env value is t then the self.address shpuld be test url address , but if the env value is not passed then the self.address value should be the key3 value from the dictionary value. These production url address and test url address are the key1 and key2 of the dictionary value

I came up with this code, but this seems to be not working.

4
  • 1
    What error message do you get? You have a strange condition if envType is None: That should of course be if env is None: Commented Mar 19, 2019 at 5:31
  • Don’t use ˋdict` as a variable. It is a builtin function. Choose a different name. Commented Mar 19, 2019 at 5:33
  • 1
    Why not ..., env=''): ? Commented Mar 19, 2019 at 5:33
  • edited the code Commented Mar 19, 2019 at 5:40

1 Answer 1

3

Try this:

def __init__(self, user, url, env=None):
    """init method for the class."""
    self.user = user
    self.url = url
    if env is None:
        self.env = ""
    else:
        self.env = env

def check_file(self):
    # get the env value from a dictionary
    if self.env: # if not an empty string
        if self.env == "test":
            self.address = value['key1']
        else:
            self.address = value['key2']
    else:
        self.address = value['key3']
Sign up to request clarification or add additional context in comments.

Comments

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.