[Tutor] Re: dynamically creating files

Don Arnold darnold02 at sprynet.com
Fri Nov 14 19:01:59 EST 2003

----- Original Message -----
From: "Andrei" <project5 at redrival.net>
To: <tutor at python.org>
Sent: Friday, November 14, 2003 7:14 AM
Subject: [Tutor] Re: dynamically creating files

> Don Arnold wrote on Fri, 14 Nov 2003 06:39:47 -0600:
> <snip>
> >> I am creating a program and have come to a problem.
> >> I wish to write a class that handles file objects, ie opening, writing
> >> and closing a file. I wish to dynamically create the file name, as each
> >> instance would have its own file too use for processing. I am not sure
> >> what syntax is required to derive a filename from the 'self' instance.
> >> Any tips/ TIA
> >>
> >
> > Would something like this work? It's not too thoroughly tested, but it
> > be a starting point  :
> >
> <snip>
> > class Filer(object):
> >     def __init__(self,targetDir):
> >         self.myfile, self.myfilename = getNewFile(targetDir)
> >         print 'using ', self.myfilename
> >     def writeLines(self):
> >         for i in range(10):
> >             txt = 'writing line %d to %s...' % (i, self.myfilename)
> <snip>
> That's a wrapper for the file class which doesn't do a lot more than the
> file class itself....

True. The behaviour I was focusing on was each instance having its own
unique file.

>  If it's necessary to have lines appear on the screen as
> well as in the file, it's better to subclass the file class and add that
> behaviour instead of making a new one from scratch. E.g.:


That's behaviour I introduced in my example as throwaway code just
to give the instance something to do.  I'm not sure if the OP wanted a
'has a' or an 'is a' relationship, so I opted for aggregation.


More information about the Tutor mailing list