473,394 Members | 1,765 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 473,394 software developers and data experts.

Creating objects you don't actually want

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
6 1218
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
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
> 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
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
sid
me feels a bit stupid:-)

Thanks for the observation.

Jul 18 '05 #6
sid
me feels a bit stupid:-)

Thanks for the observation.



Jul 18 '05 #7

This thread has been closed and replies have been disabled. Please start a new discussion.

Similar topics

2
by: PK | last post by:
Hello, I am looking for help on the following. I'm trying to create a custom browser toolbar button that will do a few things. One that I'm trying to do at the moment is just simply return the...
161
by: KraftDiner | last post by:
I was under the assumption that everything in python was a refrence... so if I code this: lst = for i in lst: if i==2: i = 4 print lst I though the contents of lst would be modified.....
9
by: Brian | last post by:
I have a question that some may consider silly, but it has me a bit stuck and I would appreciate some help in understanding what is going on. For example, lets say that I have a class that...
31
by: JoeC | last post by:
I have read books and have ideas on how to create objects. I often create my own projects and programs. They end up getting pretty complex and long. I often use objects in my programs they are...
3
by: Bartholomew Simpson | last post by:
I am writing some C++ wrappers around some legacy C ones - more specifically, I am providing ctors, dtors and assignment operators for the C structs. I have a ton of existing C code that uses...
0
by: Charles Arthur | last post by:
How do i turn on java script on a villaon, callus and itel keypad mobile phone
0
by: ryjfgjl | last post by:
If we have dozens or hundreds of excel to import into the database, if we use the excel import function provided by database editors such as navicat, it will be extremely tedious and time-consuming...
0
by: emmanuelkatto | last post by:
Hi All, I am Emmanuel katto from Uganda. I want to ask what challenges you've faced while migrating a website to cloud. Please let me know. Thanks! Emmanuel
0
BarryA
by: BarryA | last post by:
What are the essential steps and strategies outlined in the Data Structures and Algorithms (DSA) roadmap for aspiring data scientists? How can individuals effectively utilize this roadmap to progress...
1
by: nemocccc | last post by:
hello, everyone, I want to develop a software for my android phone for daily needs, any suggestions?
0
by: Hystou | last post by:
There are some requirements for setting up RAID: 1. The motherboard and BIOS support RAID configuration. 2. The motherboard has 2 or more available SATA protocol SSD/HDD slots (including MSATA, M.2...
0
marktang
by: marktang | last post by:
ONU (Optical Network Unit) is one of the key components for providing high-speed Internet services. Its primary function is to act as an endpoint device located at the user's premises. However,...
0
Oralloy
by: Oralloy | last post by:
Hello folks, I am unable to find appropriate documentation on the type promotion of bit-fields when using the generalised comparison operator "<=>". The problem is that using the GNU compilers,...
0
tracyyun
by: tracyyun | last post by:
Dear forum friends, With the development of smart home technology, a variety of wireless communication protocols have appeared on the market, such as Zigbee, Z-Wave, Wi-Fi, Bluetooth, etc. Each...

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.