2

I am accessing data from different accounts from an online platform over their API. I have created a class called Account that holds all the information necessary to access this API. I want to be able to set the account (and the necessary info to gain access) each time before I make an API request. I tried to make a function that will set a global variable Acct to the proper account class instance but after I call choose_account(), Acct continues to return '', is there a better way to handle this type of procedure?

Acct = ''
def choose_account():
    global Acct
    get = raw_input(r'Adap1, Adap2, Adap3, or Adap4? ')
    if get == 'Adap1':
        Acct = Adap1
    elif get == 'Adap2':
        Acct = Adap2
    elif get == 'Adap3':
        Acct = Adap3
    elif get == 'Adap4':
        Acct = Adap4
    else:
        print ("Please type Adap1, Adap2, Adap3, or Adap4 ")

Edit: show Account and Adap1 etc

class Account():

    def __init__(self, name, username, password, org_id):
        self.name = name
        self.username = username
        self.password = password
        self.org_id = org_id

    def update_pw(self, pw):
        self.password = pw 

Adap1 = Account('Adap1', 'username', 'password', 'org_id')
7
  • 1
    where are you setting Adap1,2,3, and 4? They look like they're empty from that code. Commented Aug 21, 2015 at 16:27
  • How are you defining Adap1, Adap2, Adap3, Adap4? Right now you are treating them as variables in your assignment Acct = Adap1, but I suspect you may have wanted to assign Acct to the string 'Adap1'? Commented Aug 21, 2015 at 16:27
  • But if that's the case, you could just do Acct = get if get in ('Adap1', ...) else None. Commented Aug 21, 2015 at 16:28
  • Good point @Kevin but I wan't to set the value of Acct to a class instance of Account so it doesn't work haha Commented Aug 21, 2015 at 16:30
  • If you don't mean to have Adap1, Adapt2... be strings and they are indeed defined somewhere else, then you need to either pass them into your choose_account() fuction, or have them be global as well Commented Aug 21, 2015 at 16:31

1 Answer 1

4

Sorry, but use of global variables in that way is not usually a good way to go. You are probably new to programming, so I don't want you to feel you are being "told off", but it would be much more sensible to have the function return a value, and then set the global variable with a statement like

Acct = choose_account()

In which case your function ought to look more like this (untested code):

def choose_acct():
    while True:
        get = raw_input(r'Adap1, Adap2, Adap3, or Adap4? ')
        if get == "Adap1":
            return Adap1
        elif get == "Adap2":
            return Adap2
        elif get == "Adap3":
            return Adap3
        elif get == "Adap4":
            return Adap4

Better still, you could consider a data-driven approach to the problem, and define a dictionary like

adict = {"Adap1": Adap1, "Adap2": Adap2, "Adap3": Adap3, "Adap4": Adap4}

Then your function could read (again, untested)

def choose_acct():
    while True:
        get = raw_input(r'Adap1, Adap2, Adap3, or Adap4? ')
        result = adict.get(get, None)
        if result:
            return result

As your experience level grows you will start to recognise the difference between good and bad code, but you made a pretty good attempt.

Sign up to request clarification or add additional context in comments.

3 Comments

Thanks, really appreciate the feedback!
I see this "while True:" line in a lot of code but I am not really sure why it is necessary or what advantage it brings, would you mind explaining that to me a bit?
Typically, you will see while True: when you want a piece of code to never terminate or accompanied by a break or return inside the loop (like in this case). It's just a way to repeat a set of commands over and over. Some might recommend against using it with a break or return (and just having the break/return condition be what replaces the True), but sometimes while True: just results in more readable code, which is a very good thing in my opinion.

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.