Creating objects you don't actually want

Discussion in 'Python' started by Chris Lyon, Feb 10, 2004.

  1. Chris Lyon

    Chris Lyon Guest

    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.
     
    Chris Lyon, Feb 10, 2004
    #1
    1. Advertising

  2. Chris Lyon

    anton muhin Guest

    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.
     
    anton muhin, Feb 10, 2004
    #2
    1. Advertising

  3. Chris Lyon

    sid Guest

    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.
     
    sid, Feb 10, 2004
    #3
  4. Chris Lyon

    Chris Lyon Guest

    > 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 !
     
    Chris Lyon, Feb 10, 2004
    #4
  5. 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
     
    Scott David Daniels, Feb 10, 2004
    #5
  6. Chris Lyon

    sid Guest

    me feels a bit stupid:)

    Thanks for the observation.
     
    sid, Feb 11, 2004
    #6
  7. Chris Lyon

    sid Guest

    me feels a bit stupid:)

    Thanks for the observation.
     
    sid, Feb 11, 2004
    #7
    1. Advertising

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

It takes just 2 minutes to sign up (and it's free!). Just click the sign up button to choose a username and then you can ask your own questions on the forum.
Similar Threads
  1. Just that
    Replies:
    6
    Views:
    364
    Victor Bazarov
    Mar 21, 2005
  2. S_K
    Replies:
    1
    Views:
    1,421
    Peter Bromberg [C# MVP]
    Jan 9, 2008
  3. Replies:
    41
    Views:
    1,243
  4. Shraddha Kapse
    Replies:
    0
    Views:
    631
    Shraddha Kapse
    May 5, 2009
  5. Jason
    Replies:
    0
    Views:
    204
    Jason
    Jul 6, 2004
Loading...

Share This Page