[Tutor] Class instance understanding = None

David david at abbottdavid.com
Fri Feb 27 18:51:56 CET 2009


Andre Engels wrote:

> 
> The more preferable method is to leave the class alone, and call
> getbalance by hand:
> 
> data = float('100.00')
> a = Account(data)
> p = a.getbalance()
> print 'balance = ', p
> remove_data = float('50.00')
> a.withdraw(remove_data)
> w = a.getbalance()
> print "withdraw = ", w
> add_data = float('50.00')
> a.deposit(add_data)
> add = a.getbalance()
> print "deposit = ", add
> 
> Some other things:
> 1. data = float('100.00') is unnecessarily clumsy - you can specify
> floats directly without creating a string first by doing data = 100.0
> 2. You are creating a lot of variables only to use them for the one
> and only time on the next line. That's not necessarily bad, it
> sometimes improves readability especially if a lot is being done
> (which can then be split up in more readable parts), but doing it this
> much mostly causes your coding to look more complicated than it
> actually is. I would either prefer something like this:
> 
> data = 100.0
> remove_data = 50.0
> add_data = 50.0        # first all input-like elements, so I know
> where to go when I want to change something trivial
> a = Account(data)
> print 'balance = ',a.getbalance()
> a.withdraw(remove_data)
> print 'balance after withdraw = ',a.getbalance()
> a.deposit(add_data)
> print 'balance after deposit = ',a.getbalance()
> 
> doing away with p, w and add, or the even shorter variant where data,
> remove_data and add_data are also removed:
> 
> a = Account(100.0)
> print 'balance = ',a.getbalance()
> a.withdraw(50.0)
> print 'balance after withdraw = ',a.getbalance()
> a.deposit(50.0)
> print 'balance after deposit = ',a.getbalance()
> 
Ok almost there, here is what i have now;
http://linuxcrazy.pastebin.com/m6b090d2d

My problem now is the balance is updated from the file that is pickled 
fine, but the first entry goes is not added to that total. I know it is 
in this part;

start_total()
start = 0
a = Account(start)

but when I change it to;
start_total()
start = start_total()
a = Account(start)

here is the error;
Enter Amount: 100
Traceback (most recent call last):
   File "./py_pettycash.py", line 77, in <module>
     menu()
   File "./py_pettycash.py", line 53, in menu
     a.deposit(cash)
   File "./py_pettycash.py", line 15, in deposit
     self.balance = self.balance + amt
TypeError: unsupported operand type(s) for +: 'NoneType' and 'Decimal'

-david


-- 
Powered by Gentoo GNU/LINUX
http://www.linuxcrazy.com
pgp.mit.edu



More information about the Tutor mailing list