By using this site, you agree to our updated Privacy Policy and our Terms of Use. Manage your Cookies Settings.
440,621 Members | 1,104 Online
Bytes IT Community
+ Ask a Question
Need help? Post your question and get tips & solutions from a community of 440,621 IT Pros & Developers. It's quick & easy.

Creating objects you don't actually want

P: n/a
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.
Jul 18 '05 #1
Share this Question
Share on Google+
6 Replies


P: n/a
Chris Lyon wrote:
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.
Jul 18 '05 #2

P: n/a
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.


Jul 18 '05 #3

P: n/a
> You shouldn't delete sfx --- it won't exist.

aaah I see !!
class fred: .... def __init__(self,param = None):
.... if param:
.... raise ValueError
.... f = fred()
f <__main__.fred instance at 0118B93C> g = fred(1) Traceback (most recent call last):
File "<interactive input>", line 1, in ?
File "<interactive input>", line 4, in __init__
ValueError g Traceback (most recent call last):
File "<interactive input>", line 1, in ?
NameError: name 'g' is not defined

Thank you very much, I appear to have assumed once more !
Jul 18 '05 #4

P: n/a
sid wrote:
... 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.


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

--
-Scott David Daniels
Sc***********@Acm.Org
Jul 18 '05 #5

P: n/a
sid
me feels a bit stupid:-)

Thanks for the observation.

Jul 18 '05 #6

P: n/a
sid
me feels a bit stupid:-)

Thanks for the observation.



Jul 18 '05 #7

This discussion thread is closed

Replies have been disabled for this discussion.