1

I have a class that is taking in an Id and trying to update the variable current_account but when I print out the details of the current_account it hasn't updated.

Anyone got any ideas for this? New to python so might be doing something stupid that I can't see.

class UserData:
    def __init__(self, db_conn=None):
        if None == db_conn:
            raise Exception("DB Connection Required.")

        self.db = db_conn
        self.set_my_account()
        self.set_accounts()
        self.set_current_account()

    def set_current_account(self, account_id=None):
        print account_id
        if None == account_id:
            self.current_account = self.my_account
        else:
            if len(self.accounts) > 0:
                for account in self.accounts:
                    if account['_id'] == account_id:
                        self.current_account = account
                        print self.current_account['_id']
            else:
                raise Exception("No accounts available.")

Assume that set_my_account() gets a dictionary of account data and that set_accounts() get a list of dictionaries of account data.

So when I do the following:

user_data = UserData(db_conn=db_conn)
user_data.set_current_account(account_id=account_id)

Where db_conn is a valid database connection and account_id is a valid account id.

I get the following out of the above two lines.

None
518a310356c02c0756764b4e
512754cfc1f3d16c25c350b7

So the None value is from the declaration of the class and then the next two are from the call to set_current_account(). The first id value is what I'm trying to set. The second id value is what was already set from the class __init__() method.

5
  • 3
    Note that None == account_id is hardly idiomatic Python. None is a singleton object, use if account_id is None: to test for it. Commented May 9, 2013 at 14:16
  • 3
    For the record, your question is about an instance variable rather than a class variable, and when defining your own classes you should always inherit from object, like this: class UserData(object): Commented May 9, 2013 at 14:18
  • Thanks for the info, will update the class as suggested. Commented May 9, 2013 at 14:25
  • On-topic, I can't replicate your behavior. When I run your code (modified by setting my_account and accounts to a simple dict and list of dicts in the initializer), it works as expected. Is there anything else you can tell us about how the code is being used that would help reproduce the problem? Can you provide a full minimal example that displays the incorrect behavior? Commented May 9, 2013 at 14:29
  • I'll see what I can do. Commented May 9, 2013 at 14:47

2 Answers 2

3

There were a lot of redundancies an un-Pythonic constructions. I cleaned up the code to help me understand what you trying to do.

class UserData(object):
    def __init__(self, db_conn):
        self.db = db_conn
        self.set_my_account()
        self.set_accounts()
        self.set_current_account()

    def set_current_account(self, account_id=None):
        print account_id
        if account_id is None:
            self.current_account = self.my_account
        else:
            if not self.accounts:
                raise Exception("No accounts available.")

            for account in self.accounts:
                if account['_id'] == account_id:
                   self.current_account = account
                   print self.current_account['_id']

user_data = UserData(db_conn)
user_data.set_current_account(account_id)

You used default arguments (db_conn=None) when a call without an explicit argument is invalid. Yes, you can now call __init__(None) but you could also call __init__('Nalum'); you can't protect against everything.

By moving the "No accounts" exception the block fast-fails and you save one level of indention.

The call UserData(db_conn=db_conn) is valid but unecessarily repetitive.

Unfortunately, I still can't figure out what you are trying to accomplish and this is perhaps the largest flaw. Variable names are terribly important for help the reader (which may be the future you) make sense of code. current_account, my_account, account_id and current_account['_id'] so obscure the intention that you should really consider more distinct, informative names.

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

1 Comment

Thanks for the feedback, I've updated my code to reflect the info. I'm coming from a PHP background, so I guess I'm bringing some of that into my code. What I'm trying to do is create an account switcher. So my_account is the logged in users account and the current_account is what the system reads from so the user can switch accounts. Perhaps there is a better way of doing it. account_id is literally an ID of the account we want to switch to.
1

Figured out what it was.

The data was being changed else where in the code base. It is now working as expected.

Thanks guys for pointing out the Python centric things that I was doing wrong, good to get it.

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.