calling __init__ more than once?

Al Gonzalez alberto at mindspring.com
Sun Sep 2 20:49:00 EDT 2001


There doesn't seem to be a reason why you can't.

I prefer to move things into a method that can be called
from multiple places including __init__.  This keeps me
from accidentally doing things again that I only want to
happen when the class is created - such as gathering
resources..

class myParser:
    def __init__(self):
        self._prepareEnvironment()
    def parse(self, fileName):
        self._prepareEnvironment()
        # do stuff with fileName
    def _prepareEnvironment(self):
        # utility method to clear up variables
        self._fileName = None
        self._buffer = []

"Emile van Sebille" <emile at fenx.com> wrote in message
news:9muijb$47a46$1 at ID-11957.news.dfncis.de...
> One thing you could try is to restructure slightly, passing the filename
on
> initialization:
>
> stuff1 = myParser(fileName1)
> stuff1.parse()
> stuff2 = myParser(fileName2)
> stuff2.parse()
>
> HTH,
>
> --
>
> Emile van Sebille
> emile at fenx.com
>
> ---------
> "Roy Smith" <roy at panix.com> wrote in message
> news:roy-5FC394.18555502092001 at news1.panix.com...
> > I've got a file parsing class which in which I call a single parser
object
> > repeatedly to parse additional files.  Something like:
> >
> >    p = myParser()
> >    stuff1 = p.parse (fileName1)
> >    stuff2 = p.parse (fileName2)
> >
> > Is there any reason why my parse method couldn't call __init__() to
reset
> > things back to the start state?  It feels kind of creepy, but I don't
see
> > any reason why it should be a problem.  Is there something I'm missing
> > here, or is it really OK to call __init__() again?
>





More information about the Python-list mailing list