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 2 1091
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
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
This thread has been closed and replies have been disabled. Please start a new discussion. Similar topics
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...
|
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.
...
|
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...
|
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...
|
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...
|
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...
|
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...
|
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...
|
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...
|
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...
|
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...
|
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...
|
by: Kemmylinns12 |
last post by:
Blockchain technology has emerged as a transformative force in the business world, offering unprecedented opportunities for innovation and...
|
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...
|
by: antdb |
last post by:
Ⅰ. Advantage of AntDB: hyper-convergence + streaming processing engine
In the overall architecture, a new "hyper-convergence" concept was...
|
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.
...
|
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...
|
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...
| |