[Tutor] Handling Objects

Kent Johnson kent37 at tds.net
Thu Oct 6 01:20:55 CEST 2005

Eric Walker wrote:
> Well,
> I think I probably can do this easier in perl but I took a vow I would try and 
> learn python.  I know I am using classes here and really don't need objects. 
> This is just another way for me to learn how to work with classes within 
> python. My object actually will be storing like 5 or 6 different attributes 
> but I didn't include them in the example.  These attributes  will be certain 
> things that are read from the file.  Once I get the objects i want to create 
> in another directory the same files with the same names but put different 
> data into the new files depending on what I read from the original files.

OK, I would just make a list of the objects, since one of the attributes is the name you have everything you need in the object.

def getNames():
    import os
    temp=currentDir + '/TEMP'
    data = []
    for name in baseList:
        print name
    return data

then to use the data something like
for datum in data:
    f = open(datum.name, 'w')


> Python Newbie....
> On Wednesday 05 October 2005 04:29 pm, Kent Johnson wrote:
>>Eric Walker wrote:
>>>New to Python and trying to do some class stuff with a simple task.
>>>1) get a list of file names in a directory
>>>2) create variables with the same name of each filename pulled from the
>>>3) Create an object for each and pass into the __init__ method the
>>>stringname of the file name.
>>>This way I get a collection of objects that are the same name as the file
>>>name and within each instance of the class , a particular attribute will
>>>have the string name of the object.  Hope this isn't too confusing..
>>What will you do with the names and objects once you have them? A better
>>approach is probably to keep a dictionary that maps names to objects. If
>>your object is really just storing the name you might as well just keep a
>>list of names - the object isn't adding any value. If the object is going
>>to have more behaviour then use a dict. If you really just want to print
>>the names then you don't need to store them at all. For example with a
>>class TPROJ:
>>    # as before
>>def getNames():
>>    import os
>>    currentDir=os.getcwd()
>>    temp=currentDir + '/TEMP'
>>    os.chdir(temp)
>>    baseList=os.listdir(".")
>>    nameDict = {}
>>    for name in baseList:
>>        nameDict[name] = TPROJ(name)
>>        print name
>>    return nameDict
>>>class TPROJ:
>>>    def __init__(self,value):#createMethod auto executes since it has __
>>>        self.BASENAME = value
>>>   def display(self):#display method
>>>        print self.BASENAME
>>>def getNames():
>>>    import os
>>>    currentDir=os.getcwd()
>>>    temp=currentDir + '/TEMP'
>>>    os.chdir(temp)
>>>    baseList=os.listdir(".")
>>>    for name in baseList:
>>>        name = TPROJ(name)
>>>        print name
>>>Can anyone see what I am trying to do?
>>>Python Newbie.......
>>>Tutor maillist  -  Tutor at python.org
>>Tutor maillist  -  Tutor at python.org

More information about the Tutor mailing list