Python class variable not updating -
i have class taking in id , trying update variable current_account when print out details of current_account hasn't updated.
anyone got ideas this? new python might doing stupid 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: 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 set_my_account()
gets dictionary of account data , set_accounts()
list of dictionaries of account data.
so when following:
user_data = userdata(db_conn=db_conn) user_data.set_current_account(account_id=account_id)
where db_conn
valid database connection , account_id
valid account id.
i following out of above 2 lines.
none 518a310356c02c0756764b4e 512754cfc1f3d16c25c350b7
so none
value declaration of class , next 2 call set_current_account()
. first id
value i'm trying set. second id
value set class __init__()
method.
there lot of redundancies un-pythonic constructions. cleaned code me understand trying 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 none: self.current_account = self.my_account else: if not self.accounts: raise exception("no accounts available.") 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 call without explicit argument invalid. yes, can call __init__(none)
call __init__('nalum')
; can't protect against everything.
by moving "no accounts" exception block fast-fails , save 1 level of indention.
the call userdata(db_conn=db_conn) valid unecessarily repetitive.
unfortunately, still can't figure out trying accomplish , perhaps largest flaw. variable names terribly important reader (which may future you) make sense of code. current_account
, my_account
, account_id
, current_account['_id']
obscure intention should consider more distinct, informative names.
Comments
Post a Comment