Creating objects you don't actually want

sid sidharthk at hotmail.com
Tue Feb 10 07:45:42 EST 2004


Python uses a reference counting mechanism, so an object will be deleted as
it there aren't any references to it. So you really needn't bother about
deleting objects.

Chris Lyon
> class sfxobject(object):
>     def __init__(self,filename):
>         if isfile(filename):
>             self.filename = filename
>             # all kinds of other parameter setting
>          else:
>             raise sfxobjectError
>
> for item in os.listdir('dir'):
>     try:
>         sfx = sfxobject(item)
>     except: sfxobjectError
>         del sfx
>
you won't be able to delete sfx since the variable wasn't even created
this could be written as
for item in os.lsitdir('dir')
    try:
        sfx = sfxobject(item)
    except sfxobjectError:
        pass


> for item in os.listdir('dir'):
>     if os.isfile(item):
>         sfx = sfxobject(item)
>
this is a better way.







More information about the Python-list mailing list