Hi,
environment: Python 2.4, GNU/Linux, kernel 2.6.12.2
having subclassed 'Exception' I'm trying to call the initialiser
__init__(...) of the superclass Exception with 'super(..).__in it__(..)' .
However, trying to do so results in a
'TypeError: super() argument 1 must be type, not classobj'.
Now, if I use 'Exception.__in it__(..)' instad of super(..)... ,everything
works just as one would expect.
Why does 'super(..).__in it__(..)' fail?
thank you for any suggestions
chriss
Here is some example code to illustrate the point:
class WorkingExceptio n(Exception):
def __init__(self, message):
# works as I would expect
Exception.__ini t__(self, message)
class BrokenException (Exception):
def __init__(self, message):
# fails with a typeError
super(BrokenExc eption, self).__init__( self, message)
# --------- case 1 ---------
try:
raise WorkingExceptio n("Hello WorkingExceptio n")
except WorkingExceptio n, e:
print e
# --------- case 3 ---------
try:
raise BrokenException ("Hello BrokenException ")
except BrokenException , e:
print e 3 5149
chriss wrote: Hi,
environment: Python 2.4, GNU/Linux, kernel 2.6.12.2
having subclassed 'Exception' I'm trying to call the initialiser __init__(...) of the superclass Exception with 'super(..).__in it__(..)' . However, trying to do so results in a 'TypeError: super() argument 1 must be type, not classobj'.
Now, if I use 'Exception.__in it__(..)' instad of super(..)... ,everything works just as one would expect.
Why does 'super(..).__in it__(..)' fail?
Exceptions do not inherit from 'object'; they are old-style classes.
super() can be used only with new-style classes (which subclass 'object').
-Peter
chriss wrote: Hi,
environment: Python 2.4, GNU/Linux, kernel 2.6.12.2
having subclassed 'Exception' I'm trying to call the initialiser __init__(...) of the superclass Exception with 'super(..).__in it__(..)' . However, trying to do so results in a 'TypeError: super() argument 1 must be type, not classobj'.
Now, if I use 'Exception.__in it__(..)' instad of super(..)... ,everything works just as one would expect.
Why does 'super(..).__in it__(..)' fail?
AFAICT, the Exception hierarchy are still old-style classes while
super() only works on new-style classes.
--
Robert Kern rk***@ucsd.edu
"In the fields of hell where the grass grows high
Are the graves of dreams allowed to die."
-- Richard Harter
Peter Hansen wrote: chriss wrote: Hi,
environment: Python 2.4, GNU/Linux, kernel 2.6.12.2
having subclassed 'Exception' I'm trying to call the initialiser __init__(...) of the superclass Exception with 'super(..).__in it__(..)' . However, trying to do so results in a 'TypeError: super() argument 1 must be type, not classobj'.
Now, if I use 'Exception.__in it__(..)' instad of super(..)... ,everything works just as one would expect.
Why does 'super(..).__in it__(..)' fail?
Exceptions do not inherit from 'object'; they are old-style classes.
super() can be used only with new-style classes (which subclass 'object').
-Peter
That explains it all right.
Thank you very much for your answer.
chriss This thread has been closed and replies have been disabled. Please start a new discussion. Similar topics |
by: daishi |
last post by:
Hi,
I am wondering if someone could help me understand some of the
details of using metaclasses.
I am trying to use metaclasses to auto-generate some attributes
and methods in a class definition. I am attaching a simplified
example of the kind of code I am considering below, and I have
the following questions:
|
by: Chris Green |
last post by:
Good day,
I've done a bit of searching in the language reference and a couple
pages referring the behavior of super() but I can't find any
discussion of why super needs the name of the class as an argument.
Feel free to point me into the bowels of google if this has been
discussed to death already.
super(self).method() seems like super could just do the right
thing...
|
by: bonono |
last post by:
Hi,
Suppose my class definition is like this :
class A:
name = "A"
@classmethod
def foo(cls):
cls.__super.foo()
|
by: Andrew Jaffe |
last post by:
Hi,
I have a class with various class-level variables which are used to
store global state information for all instances of a class. These are
set by a classmethod as in the following (in reality the setcvar method
is more complicated than this!):
class sup(object):
cvar1 = None
cvar2 = None
|
by: Christoph Zwerschke |
last post by:
Usually, you initialize class variables like that:
class A:
sum = 45
But what is the proper way to initialize class variables if they are the
result of some computation or processing as in the following silly
example (representative for more:
class A:
| |
by: ddtl |
last post by:
Hello everybody.
Consider the following code:
class A(object):
def met(self):
print 'A.met'
class B(A):
def met(self):
|
by: Per Bull Holmen |
last post by:
Hey
Im new to c++, so bear with me. I'm used to other OO languages, where it
is possible to have class-level initialization functions, that
initialize the CLASS rather than an instance of it. Like, for instance
the Objective-C method:
+(void)initialize
Which has the following characteristics: It is guaranteed to be run
|
by: Steven D'Aprano |
last post by:
Here's a simple class-factory function that returns a sub-class of the
old-style class it is passed.
def verbosify_oclass(klass):
"""Returns a verbose sub-class of old-style klass."""
class VClass(klass):
def __init__(self, *args, **kwargs):
print "Calling initializer __init__ ..."
klass.__init__(self, *args, **kwargs)
return VClass
|
by: Gordon |
last post by:
I want to provide a set of static functions in a superclass that work
with class constants defined in a decendant of that class.
Unfortunately I've run into a snag with this idea. Example:
class SuperClass
{
const CNST = 'Super class';
public static function getCnst ()
{
|
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: agi2029 |
last post by:
Let's talk about the concept of autonomous AI software engineers and no-code agents. These AIs are designed to manage the entire lifecycle of a software development project—planning, coding, testing, and deployment—without human intervention. Imagine an AI that can take a project description, break it down, write the code, debug it, and then launch it, all on its own....
Now, this would greatly impact the work of software developers. The idea...
|
by: isladogs |
last post by:
The next Access Europe User Group meeting will be on Wednesday 1 May 2024 starting at 18:00 UK time (6PM UTC+1) and finishing by 19:30 (7.30PM).
In this session, we are pleased to welcome a new presenter, Adolph Dupré who will be discussing some powerful techniques for using class modules.
He will explain when you may want to use classes instead of User Defined Types (UDT). For example, to manage the data in unbound forms.
Adolph will...
|
by: adsilva |
last post by:
A Windows Forms form does not have the event Unload, like VB6. What one acts like?
|
by: 6302768590 |
last post by:
Hai team
i want code for transfer the data from one system to another through IP address by using C# our system has to for every 5mins then we have to update the data what the data is updated we have to send another system
| |
by: muto222 |
last post by:
How can i add a mobile payment intergratation into php mysql website.
| | |