468,301 Members | 1,479 Online
Bytes | Developer Community
New Post

Home Posts Topics Members FAQ

Post your question to a community of 468,301 developers. It's quick & easy.

proposal: give delattr ability to ignore missing attribute

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
Jun 27 '08 #1
2 1017
Lie
On Jun 10, 10:06*pm, Gary Wilson <gary.wil...@gmail.comwrote:
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
Jun 27 '08 #2
On Jun 10, 4:55*pm, Lie <Lie.1...@gmail.comwrote:
On Jun 10, 10:06*pm, Gary Wilson <gary.wil...@gmail.comwrote:
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
Jun 27 '08 #3

This discussion thread is closed

Replies have been disabled for this discussion.

Similar topics

30 posts views Thread by Hallvard B Furuseth | last post: by
13 posts views Thread by Ian Hickson | last post: by
9 posts views Thread by corey.coughlin | last post: by
23 posts views Thread by dhtmlkitchen | last post: by
4 posts views Thread by MonkeeSage | last post: by
reply views Thread by NPC403 | last post: by
By using this site, you agree to our Privacy Policy and Terms of Use.