class Stock: name = String('name') shares = Integer('shares') price = Float('price') def __init__(self, name, shares, price): self.name = name self.shares = shares self.price = price # @property # def shares(self): # return self._shares # @shares.setter # def shares(self, value): # if not isinstance(value, int): # raise TypeError('expected an integer') # self._shares = value def cost(self): return self.shares * self.price def sell(self, n): if self.shares - n >= 0: self.shares -= n return self.shares def __repr__(self): return f'Stock(\'{self.name}\', {self.shares}, {self.price})'
class Stock: name = String('name') shares = Integer('shares') price = Float('price') def __init__(self, name, shares, price): self.name = name self.shares = shares self.price = price def __repr__(self): return f'Stock({self.name!r}, {self.shares!r}, {self.price!r})' @property def cost(self): ''' Return the cost as shares*price ''' return self.shares * self.price def sell(self, nshares): ''' Sell a number of shares and return the remaining number. ''' self.shares -= nshares
class Stock: name = String('name') shares = Integer('shares') price = Float('price') #__slots__ = ('name','_shares','price') def __init__(self,name,shares,price): self.name = name self.shares = shares self.price = price def __repr__(self) -> str: return f'Stock({self.name!r},{self.shares!r},{self.price!r})' @property def shares(self): return self._shares @shares.setter def shares(self,value): if not isinstance(value,int): raise TypeError('Expected int') self._shares = value @property def cost(self): return self.shares * self.price def sell(self,numshares): self.shares -= numshares
class Stock(object): name = String('name') shares = Integer('shares') price = Float('price') def __init__(self, name, shares, price): self.name = name self.shares = shares self.price = price @property def cost(self): return self.shares * self.price def sell(self, sold): self.shares -= sold def __repr__(self): return f'Stock({self.name!r}, {self.shares!r}, {self.price!r})'