472,344 Members | 2,443 Online
Bytes | Software Development & Data Engineering Community
+ Post

Home Posts Topics Members FAQ

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

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 1091
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 thread has been closed and replies have been disabled. Please start a new discussion.

Similar topics

30
by: Hallvard B Furuseth | last post by:
Now that the '-*- coding: <charset> -*-' feature has arrived, I'd like to see an addition: # -*- str7bit:True -*- After the source file has...
11
by: Steve | last post by:
posted on: comp.lang.python emailed to: docs@python.org I have a suggestion/request that will, I think, improve the Python documentation. ...
13
by: Ian Hickson | last post by:
A group of us have been unofficially working on a proposal of extensions to HTML4's Forms chapter, and would like to get input from a wider range...
4
by: wkaras | last post by:
I would like to propose the following changes to the C++ Standard, the goal of which are to provide an improved ability to specify the constraints...
9
by: corey.coughlin | last post by:
Alright, so I've been following some of the arguments about enhancing parallelism in python, and I've kind of been struck by how hard things still...
14
by: cody | last post by:
I got a similar idea a couple of months ago, but now this one will require no change to the clr, is relatively easy to implement and would be a...
23
by: dhtmlkitchen | last post by:
JSON We all know what it is. In ECMAScript 4, there's a JSON proposal: Object.prototype.toJSONString String.prototype.parseJSON The...
4
by: MonkeeSage | last post by:
Proposal: When an attribute lookup fails for an object, check the top-level (and local scope?) for a corresponding function or attribute and...
0
by: Jerry Coffin | last post by:
In article <9f60e411-a5b1-4571-9d3d-005432378cd4@ 56g2000hsm.googlegroups.com>, aitorf666@gmail.com says... That's not the real reason for rvalue...
0
by: concettolabs | last post by:
In today's business world, businesses are increasingly turning to PowerApps to develop custom business applications. PowerApps is a powerful tool...
0
better678
by: better678 | last post by:
Question: Discuss your understanding of the Java platform. Is the statement "Java is interpreted" correct? Answer: Java is an object-oriented...
0
by: teenabhardwaj | last post by:
How would one discover a valid source for learning news, comfort, and help for engineering designs? Covering through piles of books takes a lot of...
0
by: Kemmylinns12 | last post by:
Blockchain technology has emerged as a transformative force in the business world, offering unprecedented opportunities for innovation and...
0
jalbright99669
by: jalbright99669 | last post by:
Am having a bit of a time with URL Rewrite. I need to incorporate http to https redirect with a reverse proxy. I have the URL Rewrite rules made...
0
by: antdb | last post by:
Ⅰ. Advantage of AntDB: hyper-convergence + streaming processing engine In the overall architecture, a new "hyper-convergence" concept was...
0
by: Matthew3360 | last post by:
Hi there. I have been struggling to find out how to use a variable as my location in my header redirect function. Here is my code. ...
0
hi
by: WisdomUfot | last post by:
It's an interesting question you've got about how Gmail hides the HTTP referrer when a link in an email is clicked. While I don't have the specific...
0
Oralloy
by: Oralloy | last post by:
Hello Folks, I am trying to hook up a CPU which I designed using SystemC to I/O pins on an FPGA. My problem (spelled failure) is with the...

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.