proposal: give delattr ability to ignore missing attribute

Discussion in 'Python' started by Gary Wilson, Jun 10, 2008.

  1. Gary Wilson

    Gary Wilson Guest

    I would like to propose that functionality be added to delattr to
    handle the case when the attribute does not exist.

    First off, getattr handles this nicely with the default parameter:

    value = getattr(obj, 'foo', False)

    instead of:

    try:
    value = getattr(obj, 'foo')
    except AttributeError:
    value = False

    or:

    if hasattr(obj, 'foo'):
    value = getattr(obj, 'foo')
    else:
    value = False


    And I think it makes sense to have something similar for delattr (name
    the argument as you wish):

    delattr(obj, 'foo', allow_missing=True)

    instead of:

    try:
    delattr(obj, 'foo')
    except AttributeError:
    pass

    or:

    try:
    del obj.foo
    except AttributeError:
    pass

    or:

    if hasattr(obj, 'foo')
    delattr(obj, 'foo')

    For backwards compatibility, allow_missing would default to False.

    Gary
    Gary Wilson, Jun 10, 2008
    #1
    1. Advertising

  2. Gary Wilson

    Lie Guest

    On Jun 10, 10:06 pm, Gary Wilson <> wrote:
    > I would like to propose that functionality be added to delattr to
    > handle the case when the attribute does not exist.
    >
    > First off, getattr handles this nicely with the default parameter:
    >
    > value = getattr(obj, 'foo', False)
    >
    > instead of:
    >
    > try:
    >     value = getattr(obj, 'foo')
    > except AttributeError:
    >     value = False
    >
    > or:
    >
    > if hasattr(obj, 'foo'):
    >     value = getattr(obj, 'foo')
    > else:
    >     value = False
    >
    > And I think it makes sense to have something similar for delattr (name
    > the argument as you wish):
    >
    > delattr(obj, 'foo', allow_missing=True)
    >
    > instead of:
    >
    > try:
    >     delattr(obj, 'foo')
    > except AttributeError:
    >     pass
    >
    > or:
    >
    > try:
    >     del obj.foo
    > except AttributeError:
    >     pass
    >
    > or:
    >
    > if hasattr(obj, 'foo')
    >     delattr(obj, 'foo')
    >
    > For backwards compatibility, allow_missing would default to False.
    >
    > Gary


    That doesn't need to be implemented internally, you could do it
    yourself in python.

    def my_delattr(obj, attr):
    try:
    delattr(obj, attr)
    except AttributeError:
    pass
    def my_getattr(obj, attr, default):
    try:
    return getattr(obj, attr)
    except AttributeError:
    return default
    Lie, Jun 10, 2008
    #2
    1. Advertising

  3. Gary Wilson

    Fuzzyman Guest

    On Jun 10, 4:55 pm, Lie <> wrote:
    > On Jun 10, 10:06 pm, Gary Wilson <> wrote:
    >
    >
    >
    > > I would like to propose that functionality be added to delattr to
    > > handle the case when the attribute does not exist.

    >


    I've never once needed that functionality. In fact I very rarely use
    delattr at all. I don't think there is a compelling enough use case
    for adding this to Python.

    Michael Foord
    http://www.ironpythoninaction.com/


    > > First off, getattr handles this nicely with the default parameter:

    >
    > > value = getattr(obj, 'foo', False)

    >
    > > instead of:

    >
    > > try:
    > >     value = getattr(obj, 'foo')
    > > except AttributeError:
    > >     value = False

    >
    > > or:

    >
    > > if hasattr(obj, 'foo'):
    > >     value = getattr(obj, 'foo')
    > > else:
    > >     value = False

    >
    > > And I think it makes sense to have something similar for delattr (name
    > > the argument as you wish):

    >
    > > delattr(obj, 'foo', allow_missing=True)

    >
    > > instead of:

    >
    > > try:
    > >     delattr(obj, 'foo')
    > > except AttributeError:
    > >     pass

    >
    > > or:

    >
    > > try:
    > >     del obj.foo
    > > except AttributeError:
    > >     pass

    >
    > > or:

    >
    > > if hasattr(obj, 'foo')
    > >     delattr(obj, 'foo')

    >
    > > For backwards compatibility, allow_missing would default to False.

    >
    > > Gary

    >
    > That doesn't need to be implemented internally, you could do it
    > yourself in python.
    >
    > def my_delattr(obj, attr):
    >     try:
    >         delattr(obj, attr)
    >     except AttributeError:
    >         pass
    > def my_getattr(obj, attr, default):
    >     try:
    >         return getattr(obj, attr)
    >     except AttributeError:
    >         return default
    Fuzzyman, Jun 10, 2008
    #3
    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. =?Utf-8?B?U2NhcmVjcm93?=

    Do asp.net have ability to access client CDRW/DVDRW?

    =?Utf-8?B?U2NhcmVjcm93?=, Jan 28, 2004, in forum: ASP .Net
    Replies:
    2
    Views:
    316
    Hermit Dave
    Jan 28, 2004
  2. Dan Fingerman

    BASE attribute to ignore anchor links

    Dan Fingerman, Mar 4, 2005, in forum: HTML
    Replies:
    5
    Views:
    8,539
    Dan Fingerman
    Mar 7, 2005
  3. grocery_stocker
    Replies:
    10
    Views:
    620
    Keith Thompson
    May 25, 2005
  4. MonkeeSage
    Replies:
    6
    Views:
    280
    MonkeeSage
    Nov 19, 2007
  5. Rob Meade
    Replies:
    6
    Views:
    264
    Rob Meade
    Mar 1, 2004
Loading...

Share This Page