[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
    currentDir=os.getcwd()
    temp=currentDir + '/TEMP'
    os.chdir(temp)
    baseList=os.listdir(".")
    data = []
    for name in baseList:
        data.append(TPROJ(name))
        print name
    return data

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

Kent

> 
> 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.
>>>Problem:
>>>1) get a list of file names in a directory
>>>2) create variables with the same name of each filename pulled from the
>>>directory.
>>>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..
>>>example.
>>
>>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
>>dict:
>>
>>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
>>
>>HTH,
>>Kent
>>
>>
>>>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
>>>http://mail.python.org/mailman/listinfo/tutor
>>
>>_______________________________________________
>>Tutor maillist  -  Tutor at python.org
>>http://mail.python.org/mailman/listinfo/tutor
> 
> 



More information about the Tutor mailing list