Hi,
To satisfy my curiosity I was wondering if anyone knew if this
behaviour was intentional?
Is there a specific reason why exceptions are not allowed to be new
style classes?
Python 2.3 (#46, Jul 29 2003, 18:54:32) [MSC v.1200 32 bit (Intel)] on
win32 class OldException: pass raise OldException()
Traceback (most recent call last):
File "<stdin>", line 1, in ?
__main__.OldExc eption: <__main__.OldEx ception instance at 0x006AC490>
class NewException(ob ject): pass raise NewException()
Traceback (most recent call last):
File "<stdin>", line 1, in ?
TypeError: exceptions must be classes, instances, or strings
(deprecated), not NewException 6 2027
Quoth Andrew: To satisfy my curiosity I was wondering if anyone knew if this behaviour was intentional? Is there a specific reason why exceptions are not allowed to be new style classes?
It is intentional.
There are two difficulties with allowing new-style classes as
exception types. Ideas exist for dealing with them, but at the
moment none of them as been implemented.
Problem 1: Implicit instantiation. When the interpreter sees
raise x
it must determine whether x is a class or an instance, so it can
determine whether to instantiate it. (E.g., in
x = TypeError
raise x
instantiation will occur during the raise statement, whereas in
x = TypeError()
raise x
it will not.) The problem is that, with new-style classes, there
is no clear and strong distinction between classes and instances,
so it is not clear how the interpreter can make this decision.
Problem 2: It is probably not desirable to allow things like
raise 3
raise {'d': 17}
since raising such objects as ints and dicts (etc.) is very
unlikely to be anything but a bug in practice. (There are cases
in which you might want to do such things, but they are rare.)
However, there is (or will and should be) no clear and strong
distinction between, say, user-defined new-style classes and
built-in types, so it is not clear how the interpreter would
detect such cases and forbid them.
At the moment the dominant idea for solving these problems is to
make inheritance from Exception mandatory for exception types; see
Guido's 11 June post
<http://mail.python.org/pipermail/python-dev/2003-June/036162.html>
for details. (I have another idea involving a new special method
'__raise__', but haven't worked out the details yet.)
--
Steven Taschuk w_w st******@telusp lanet.net ,-= U
1 1
Steven Taschuk wrote: At the moment the dominant idea for solving these problems is to make inheritance from Exception mandatory for exception types; see Guido's 11 June post <http://mail.python.org/pipermail/python-dev/2003-June/036162.html> for details. (I have another idea involving a new special method '__raise__', but haven't worked out the details yet.)
Why not give exceptions their own metaclass? So if type(x) is
ExceptionMetacl ass, it's a class. If type(type(x)) is
ExceptionMetacl ass, it's an instance. Otherwise, it's illegal to
raise it.
--
CARL BANKS
Quoth Carl Banks: Steven Taschuk wrote: At the moment the dominant idea for solving these problems is to make inheritance from Exception mandatory for exception types; see
[...] Why not give exceptions their own metaclass? So if type(x) is ExceptionMetacl ass, it's a class. If type(type(x)) is ExceptionMetacl ass, it's an instance. Otherwise, it's illegal to raise it.
That seems workable. (I'd prefer, though, to test
isinstance(type (x), ExceptionMetacl ass)
and likewise for the second case.)
I'm not sure what it gains us, though, over the idea of mandatory
inheritance from Exception. Am I missing something?
--
Steven Taschuk st******@telusp lanet.net
"Telekinesi s would be worth patenting." -- James Gleick
Steven Taschuk wrote: Quoth Carl Banks: Steven Taschuk wrote: > At the moment the dominant idea for solving these problems is to > make inheritance from Exception mandatory for exception types; see [...] Why not give exceptions their own metaclass? So if type(x) is ExceptionMetacl ass, it's a class. If type(type(x)) is ExceptionMetacl ass, it's an instance. Otherwise, it's illegal to raise it.
That seems workable. (I'd prefer, though, to test isinstance(type (x), ExceptionMetacl ass) and likewise for the second case.)
I'm not sure what it gains us, though, over the idea of mandatory inheritance from Exception. Am I missing something?
Probably not much, unless you think you want to raise exceptions that
aren't subclasses of Exception.
--
CARL BANKS
Steven Taschuk <st******@telus planet.net> writes: Quoth Andrew: To satisfy my curiosity I was wondering if anyone knew if this behaviour was intentional? Is there a specific reason why exceptions are not allowed to be new style classes?
It is intentional.
There are two difficulties with allowing new-style classes as exception types. Ideas exist for dealing with them, but at the moment none of them as been implemented.
They have, in PyPy! (which doesn't do old-style classes at all, at
present).
IIUC, Guido thought what PyPy does would be sensible enough for
CPython, but I don't know what the timeframe might be.
Cheers,
mwh
--
This is not to say C++ = bad, Lisp = good. It's to say
C++ = bad irrespective of everything else.
-- Alain Picard, comp.lang.lisp
Quoth Carl Banks: Steven Taschuk wrote: Quoth Carl Banks:
[...] Why not give exceptions their own metaclass? So if type(x) is ExceptionMetacl ass, it's a class. If type(type(x)) is ExceptionMetacl ass, it's an instance. Otherwise, it's illegal to raise it.
[...] I'm not sure what it gains us, though, over the idea of mandatory inheritance from Exception. Am I missing something?
Probably not much, unless you think you want to raise exceptions that aren't subclasses of Exception.
I would have thought that having to raise instances of classes
which are instances of ExceptionMetacl ass would be just as onerous
as having to raise instances of Exception.
class MyRaisable(Exce ption):
# ...
class MyRaisable(obje ct):
__metaclass__ = ExceptionMetacl ass
# ...
I suppose Exception could have behaviour you don't want, in which
case the latter might be preferable.
--
Steven Taschuk st******@telusp lanet.net
"Our analysis begins with two outrageous benchmarks."
-- "Implementa tion strategies for continuations", Clinger et al. This thread has been closed and replies have been disabled. Please start a new discussion. Similar topics |
by: dkcpub |
last post by:
I'm very new to Python, but I couldn't find anything in the docs or faq
about this. And I fished around in the IDLE menus but didn't see anything.
Is there a tool that can determine all the exceptions that can be raised
in a Python function, or in any of the functions it calls, etc.?
/Dan
|
by: Torsten Bronger |
last post by:
Hallöchen!
I write a module, and so I must raise exceptions at several points.
The PEP 8 says that a module should define their own exceptions base
class, derived from "Exception", and derivatives of that.
However, the build-in exceptions cover most of the error types that
occur in a standard program. For example, my module communicates
with measurement instruments, so any communication error would fit
perfectly to the build-in...
|
by: Dave |
last post by:
Hello all,
Unfortunately, the reference I have is a bit slim on describing how to
create user-defined exceptions derived from std::exception. I think what I
have below will work, but is it the way the mechanism was intended to be
used?
Thanks,
Dave
|
by: Razzie |
last post by:
Hi all,
The title of this post may sound a bit weird, but I was wondering about the
following nonetheless.
I have a class libray containing, say, 4 classes: A, B, C, D. Class A
somehow has a reference to B, B has a reference to C, and C to D.
If an exception happens in class D, I would like class A to get a
notification of this (all execution on classes B to D should be terminated).
I am wondering how to do this. The following seems...
|
by: cat |
last post by:
I had a long and heated discussion with other developers on my team on when
it makes sense to throw an exception and when to use an alternate solution.
The .NET documentation recommends that an exception should be thrown only in
exceptional situations. It turned out that each of my colleagues had their
own interpretation about what an "exceptional situation" may actually be.
First of all, myself I’m against using exceptions extensively,...
| |
by: Zytan |
last post by:
I know that WebRequest.GetResponse can throw WebException from
internet tutorials. However in the MSDN docs:
http://msdn2.microsoft.com/en-us/library/system.net.webrequest.getresponse.aspx
It only lists NotImplementedException in the Exceptions section.
(Note that it does mention WebException in the Remarks section, but
who knows if this is always the case for such classes, and thus
perhaps they can't be trusted to always list these, and...
|
by: Christoph Zwerschke |
last post by:
What is the best way to re-raise any exception with a message
supplemented with additional information (e.g. line number in a
template)? Let's say for simplicity I just want to add "sorry" to every
exception message. My naive solution was this:
try:
...
except Exception, e:
raise e.__class__, str(e) + ", sorry!"
|
by: RedSon |
last post by:
Chapter 3: What are the most common Exceptions and what do they mean?
As we saw in the last chapter, there isn't only the standard Exception, but you also get special exceptions like NullPointerException or ArrayIndexOutOfBoundsException. All of these extend the basic class Exception.
In general, you can sort Exceptions into two groups: Checked and unchecked Exceptions. Checked Exceptions are checked by the compiler at compilation time. Most...
|
by: Rafe |
last post by:
Hi,
I've encountered a problem which is making debugging less obvious than
it should be. The @property decorator doesn't always raise exceptions.
It seems like it is bound to the class but ignored when called. I can
see the attribute using dir(self.__class__) on an instance, but when
called, python enters __getattr__. If I correct the bug, the attribute
calls work as expected and do not call __getattr__.
I can't seem to make a simple...
|
by: marktang |
last post by:
ONU (Optical Network Unit) is one of the key components for providing high-speed Internet services. Its primary function is to act as an endpoint device located at the user's premises. However, people are often confused as to whether an ONU can Work As a Router. In this blog post, we’ll explore What is ONU, What Is Router, ONU & Router’s main usage, and What is the difference between ONU and Router. Let’s take a closer look !
Part I. Meaning of...
|
by: Hystou |
last post by:
Most computers default to English, but sometimes we require a different language, especially when relocating. Forgot to request a specific language before your computer shipped? No problem! You can effortlessly switch the default language on Windows 10 without reinstalling. I'll walk you through it.
First, let's disable language synchronization. With a Microsoft account, language settings sync across devices. To prevent any complications,...
| |
by: Oralloy |
last post by:
Hello folks,
I am unable to find appropriate documentation on the type promotion of bit-fields when using the generalised comparison operator "<=>".
The problem is that using the GNU compilers, it seems that the internal comparison operator "<=>" tries to promote arguments from unsigned to signed.
This is as boiled down as I can make it.
Here is my compilation command:
g++-12 -std=c++20 -Wnarrowing bit_field.cpp
Here is the code in...
|
by: jinu1996 |
last post by:
In today's digital age, having a compelling online presence is paramount for businesses aiming to thrive in a competitive landscape. At the heart of this digital strategy lies an intricately woven tapestry of website design and digital marketing. It's not merely about having a website; it's about crafting an immersive digital experience that captivates audiences and drives business growth.
The Art of Business Website Design
Your website is...
|
by: Hystou |
last post by:
Overview:
Windows 11 and 10 have less user interface control over operating system update behaviour than previous versions of Windows. In Windows 11 and 10, there is no way to turn off the Windows Update option using the Control Panel or Settings app; it automatically checks for updates and installs any it finds, whether you like it or not. For most users, this new feature is actually very convenient. If you want to control the update process,...
|
by: tracyyun |
last post by:
Dear forum friends,
With the development of smart home technology, a variety of wireless communication protocols have appeared on the market, such as Zigbee, Z-Wave, Wi-Fi, Bluetooth, etc. Each protocol has its own unique characteristics and advantages, but as a user who is planning to build a smart home system, I am a bit confused by the choice of these technologies. I'm particularly interested in Zigbee because I've heard it does some...
|
by: conductexam |
last post by:
I have .net C# application in which I am extracting data from word file and save it in database particularly. To store word all data as it is I am converting the whole word file firstly in HTML and then checking html paragraph one by one.
At the time of converting from word file to html my equations which are in the word document file was convert into image.
Globals.ThisAddIn.Application.ActiveDocument.Select();...
|
by: muto222 |
last post by:
How can i add a mobile payment intergratation into php mysql website.
| |
by: bsmnconsultancy |
last post by:
In today's digital era, a well-designed website is crucial for businesses looking to succeed. Whether you're a small business owner or a large corporation in Toronto, having a strong online presence can significantly impact your brand's success. BSMN Consultancy, a leader in Website Development in Toronto offers valuable insights into creating effective websites that not only look great but also perform exceptionally well. In this comprehensive...
| |