class OverdrawnError(Exception): pass class Account(object): __slot__=['name','balance','trans'] num_acs = 0 @staticmethod #technical term - decorator (annotation) def get_num_acs(): return Account.num_acs #get_num_acs = staticmethod(get_num_acs) def __init__(self, name): self.name = name self.balance = 0 self.trans = list() Account.num_acs += 1 # self._dict_['name'] = name # self._dict_['balance'] = 0 def __getitem__(self, index): pass #return self.trans[index] def __iter__(self): return iter(self.trans) def get_all_trans(self): return tuple(self.trans) def get_balance(self): return self.balance def deposit(self, amount): self.trans.append(amount) self.balance += amount def withdraw(self, amount): self.trans.append(-amount) self.balance -= amount def self_tests(): b1 = Account('joe') # rv = Account.__new__(...) # Account.__init__(rv,'joe') assert(b1.get_balance() ==0) #b1.__class__.get_balance(b1) b1.deposit(100) assert(b1.get_balance() == 100) b1.withdraw(10) assert(b1.get_balance() == 90) assert (b1 !=None) #print b1.get_num_acs() assert(b1.get_num_acs() ==1) assert(Account.get_num_acs() ==1) #Account.get_num_acs() assert(b1.get_all_trans() == [100.-10]) print b1.get_all_trans()[-2 :] import pdb pdb.set_trace() assert( b1[0] == 100) #b1.__class__.__getitem__(b1,0) print b1[0:2] for transaction in b1: print transaction list(b1) tuple(b1) b1.balance = 1000000000000000000 print b1.get_balance() print b1.balance print'ALL TESTS PASSED' if __name__ =='__main__': self_tests()
Run
Reset
Share
Import
Link
Embed
Language▼
English
中文
Python Fiddle
Python Cloud IDE
Follow @python_fiddle
Browser Version Not Supported
Due to Python Fiddle's reliance on advanced JavaScript techniques, older browsers might have problems running it correctly. Please download the latest version of your favourite browser.
Chrome 10+
Firefox 4+
Safari 5+
IE 10+
Let me try anyway!
url:
Go
Python Snippet
Stackoverflow Question