[Tutor] Class instance understanding = None

spir denis.spir at free.fr
Fri Feb 27 07:58:19 CET 2009


Le Fri, 27 Feb 2009 00:06:59 -0500,
David <david at abbottdavid.com> s'exprima ainsi:

> Hi Everyone,
> I go through the archived [Tutor] mail list to find programs others have 
> tried to do. I found one that would keep track of a petty cash fund. 
> please point out my misunderstanding.
> Here is what I started with;
> <code>
> #!/usr/bin/python
> 
> from reportlab.lib.normalDate import ND
> #import cPickle as p
> #import pprint
> 
> today = ND()
> 
> class Account:
>      def __init__(self, initial):
>          self.balance = initial
>      def deposit(self, amt):
>          self.balance = self.balance + amt
>      def withdraw(self, amt):
>          self.balance = self.balance - amt
>      def getbalance(self):
>          return self.balance
> print 'The current date is: ', today.formatUS()
> 
> 
> data = float('100.00')
> a = Account(data)
> p = a.getbalance()
> print 'balance = ', p
> remove_data = float('50.00')
> w = a.withdraw(remove_data)
> print "withdraw = ", w
> add_data = float('50.00')
> add = a.deposit(add_data)
> print "deposit = ", add
> </code>
> 
> results;
> The current date is:  02/27/09
> balance =  100.0
> withdraw =  None
> deposit =  None
> 
> expected results;
> The current date is:  02/27/09
> balance =  100.0
> withdraw =  50.0
> deposit =  100.0
> 
> thanks,
> -david

PS: I guess you misunderstand the methods withdraw and deposit: If you read the code (read it again), they *do* something, meaning they process an *action*. Understand they name as verbs. As a consequence they do not return anything result (they do not *make* anything). They are like Pascal procedures. So that you should state instead:
w = float('50.00')
###w = a.withdraw(remove_data)
print "withdraw = ", w

denis
------
la vita e estrany


More information about the Tutor mailing list