Creating objects you don't actually want

C

Chris Lyon

If you wish to create an object:-

for item in os.listdir('dir'):
sfx = sfxobject(item)


class sfxobject(object):
def __init__(self, filename):
if isfile(filename):
self.filename = filename
# all kinds of other parameter setting

If the creation fails for any reason you are left with an object which
is not really a 'proper' object how is it best to deal with this
circumstance?

Do you raise an exception and perform the creation line within a try:
except: and then delete it?


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

or should you heavily check the parameter before you try to create the
object?

for item in os.listdir('dir'):
if os.isfile(item):
sfx = sfxobject(item)

I would think that this second method should really have all the
checking within the object it's self but then of course you have to
create the object to allow you to do the checks and you will still be
left with an inapproriate object to clean up afterwards?

No doubt there are many other ways of addressing this but it's a
problem I would love to have a deffinative answer to, unless of course
there isn't one.
 
A

anton muhin

Chris said:
If you wish to create an object:-

for item in os.listdir('dir'):
sfx = sfxobject(item)


class sfxobject(object):
def __init__(self, filename):
if isfile(filename):
self.filename = filename
# all kinds of other parameter setting

If the creation fails for any reason you are left with an object which
is not really a 'proper' object how is it best to deal with this
circumstance?

Do you raise an exception and perform the creation line within a try:
except: and then delete it?


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 shouldn't delete sfx --- it won't exist.
or should you heavily check the parameter before you try to create the
object?

for item in os.listdir('dir'):
if os.isfile(item):
sfx = sfxobject(item)

I would think that this second method should really have all the
checking within the object it's self but then of course you have to
create the object to allow you to do the checks and you will still be
left with an inapproriate object to clean up afterwards?

No doubt there are many other ways of addressing this but it's a
problem I would love to have a deffinative answer to, unless of course
there isn't one.

It depends on design, in some cases you would check parameters, in some
just pass them. I generally prefer to pass parameters and catch
exceptions if something goes wrong.

anton.
 
S

sid

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.
 
C

Chris Lyon

You shouldn't delete sfx --- it won't exist.

aaah I see !!
.... def __init__(self,param = None):
.... if param:
.... raise ValueError
.... Traceback (most recent call last):
File "<interactive input>", line 1, in ?
Traceback (most recent call last):


Thank you very much, I appear to have assumed once more !
 
S

Scott David Daniels

sid said:
... this could be written as
for item in os.lsitdir('dir')
try:
sfx = sfxobject(item)
except sfxobjectError:
pass

this is a better way.

I'd disagree. Try and handle failure solves funny instances, such as
Just after os.isfile(item) a separate process deletes the file. Then
sfxobject(item) will fail, because the test tested something that was
true in the past. Just go ahead and handle failure often works better
than "look before you leap."
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

No members online now.

Forum statistics

Threads
473,769
Messages
2,569,578
Members
45,052
Latest member
LucyCarper

Latest Threads

Top