class - how to define __init__ so that it accepts a collection of objects from other classes in Python -


i'm familiar procedural programming in python. however, i've started learning classes in python bear me if sounds noob question you.

i've been following steve f. lott's book "building skills in python" , "building skills in object oriented design" , stuck when in exercises asks me to:

classname.__init__(self,name,symbol,*blocks) 

where name , symbol symbols of company.(for more complete description of problem- this i'm working on). *blocks supposed stand collection of class. till i've tried this:

class stockblock(): """define block of stock"""     def __init__(self,date,price,nos):          self.date=date          self.price=price       self.nos=nos      def __str__(self):         return "%s %d %d" %(self.date,self.price,self.nos)     def getpurchvalue(self):         self.purch_val=self.price*self.nos         return self.purch_val     def getsalevalue(self,saleprice):         self.salesval=saleprice*self.nos         return self.salesval     def getroi(self,saleprice):         self.salesval=saleprice*self.nos         roi=((self.salesval)-(self.purch_val))/(self.purch_val)         return roi     def getstock(self):         return self 

and

def position(stockblock):     def __init__(self,name,sym,*blocks):                 super(stockblock,self).__init__(*blocks)                 self.name=name                 self.sym=sym     def __str__(self):              return "symbol: %s total val:%d" %(self.sym,self.totalval)     def getpurchvalue(self):          self.totalval=0          obj in self._stocks:               self.totalval+=obj.getpurchvalue()          return self.totalval      def getsalevalue(self,saleprice):           self.totalsale=0           d in self._stocks:               self.totalsale+=d.getsalevalue(saleprice)           return self.totalsale     def getroi(self,saleprice):        self.roi=(self.getsalevalue()- self.getpurchvalue())/self.getpurchvalue()       return self.roi 

can tell me how initialize position class list of objects stockblock class?

gracias.

you need understand concepts of "inheritance" , "composition".

a "truck" special kind of "car". kind of relation, use inheritance: class truck(car).

but wheel isn't car, no matter how @ it. cat has 3 or more wheels. it's part add wheels car-type class, use composition.

in code, position isn't special kind of stockblock, instead contains collection of stockblock. need initialize class this:

def position(object):     def __init__(self,name,sym,*blocks):         self.name=name         self.sym=sym         self.blocks=blocks 

related:


Comments

Popular posts from this blog

Magento/PHP - Get phones on all members in a customer group -

php - .htaccess mod_rewrite for dynamic url which has domain names -

Website Login Issue developed in magento -