[Tutor] object design question

Kent Johnson kent37 at tds.net
Tue May 15 16:17:34 CEST 2007


Kent Tenney wrote:
> Howdy,
> 
> I would be interested in some discussion of
> which of the following approaches is preferred and why. 
> 
> class RstManager:
>     
>     def __init__(self, text):
>         self.text = text
>         self.parseRst()
>         
>     def parseRst(self):
>         parsed = <do stuff to self.text>
>         self.parsed = parsed
>         
>         
> class RstManager:
>     
>     def __init__(self, text):
>         self.parsed = parseRst(text)
>         
>     def parseRst(self, text):
>         parsed = <do stuff to text>
>         return parsed

If you have no further need of text, I prefer the second. If you need to 
keep text around then use the first one or possibly

class RstManager:

     def __init__(self, text):
         self.text = text
         self.parsed = parseRst(text)

     def parseRst(self, text):
         parsed = <do stuff to text>
         return parsed

which makes the assignment to self.parsed more explicit.

Kent

> 
> 
> Thanks,
> Kent
> 
> _______________________________________________
> Tutor maillist  -  Tutor at python.org
> http://mail.python.org/mailman/listinfo/tutor
> 



More information about the Tutor mailing list