472,993 Members | 2,460 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

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

less obvious "super"

Hi group,
I am confused with "super" usage..It seems to be complicated and less
obvious.
Here is what I need to achieve..

class A :
def __init__( self ):
self.x = 0

class B ( A ):
def __init__( self, something ):
# Use "super" construct here so that I can "inherit" x of A
self.y = something

How should I use "super" so that I could access the variable "x" of A
in B?

Thanks!

Sep 10 '07 #1
5 1366
Nagarajan <na****@gmail.comwrote:
Here is what I need to achieve..

class A :
def __init__( self ):
self.x = 0
Don't use old style classes. If you are planning to use 'super' then you
must use new-style classes, so use 'object' as a base class here.
>
class B ( A ):
def __init__( self, something ):
# Use "super" construct here so that I can "inherit" x of A
self.y = something

How should I use "super" so that I could access the variable "x" of A
in B?
If you aren't worried about diamond shaped multiple inheritance
hierarchies then just use:

class B ( A ):
def __init__( self, something ):
A.__init__(self)
self.y = something

If you are then:

class B ( A ):
def __init__( self, something ):
super(B, self).__init__()
self.y = something

When you use super you usually just want the current class and current
instance as parameters. Putting that together:
>>class A(object):
def __init__( self ):
self.x = 0

>>class B ( A ):
def __init__( self, something ):
super(B, self).__init__()
self.y = something

>>obj = B(3)
obj.x
0
>>obj.y
3
>>>
Sep 10 '07 #2
On Sep 10, 4:20 pm, Duncan Booth <duncan.bo...@invalid.invalidwrote:
Nagarajan <nag...@gmail.comwrote:
Here is what I need to achieve..
class A :
def __init__( self ):
self.x = 0

Don't use old style classes. If you are planning to use 'super' then you
must use new-style classes, so use 'object' as a base class here.
class B ( A ):
def __init__( self, something ):
# Use "super" construct here so that I can "inherit" x of A
self.y = something
How should I use "super" so that I could access the variable "x" of A
in B?

If you aren't worried about diamond shaped multiple inheritance
hierarchies then just use:

class B ( A ):
def __init__( self, something ):
A.__init__(self)
self.y = something

If you are then:

class B ( A ):
def __init__( self, something ):
super(B, self).__init__()
self.y = something

When you use super you usually just want the current class and current
instance as parameters. Putting that together:
>class A(object):

def __init__( self ):
self.x = 0
>class B ( A ):

def __init__( self, something ):
super(B, self).__init__()
self.y = something
>obj = B(3)
obj.x
0
>obj.y
3
What's the difference b/w:
class A :
and

Sep 10 '07 #3
On Sep 10, 4:20 pm, Duncan Booth <duncan.bo...@invalid.invalidwrote:
Nagarajan <nag...@gmail.comwrote:
Here is what I need to achieve..
class A :
def __init__( self ):
self.x = 0

Don't use old style classes. If you are planning to use 'super' then you
must use new-style classes, so use 'object' as a base class here.
class B ( A ):
def __init__( self, something ):
# Use "super" construct here so that I can "inherit" x of A
self.y = something
How should I use "super" so that I could access the variable "x" of A
in B?

If you aren't worried about diamond shaped multiple inheritance
hierarchies then just use:

class B ( A ):
def __init__( self, something ):
A.__init__(self)
self.y = something

If you are then:

class B ( A ):
def __init__( self, something ):
super(B, self).__init__()
self.y = something

When you use super you usually just want the current class and current
instance as parameters. Putting that together:
>class A(object):

def __init__( self ):
self.x = 0
>class B ( A ):

def __init__( self, something ):
super(B, self).__init__()
self.y = something
>obj = B(3)
obj.x
0
>obj.y
3

What's the difference b/w:
class A:
and
class A ( object ):

Thanks.

Sep 10 '07 #4
Nagarajan wrote:
class A :
def __init__( self ):
self.x = 0

class B ( A ):
def __init__( self, something ):
# Use "super" construct here so that I can "inherit" x of
# A
self.y = something

How should I use "super" so that I could access the variable "x"
of A in B?
Since you're neither using new-style classes (inheriting from
object) nor multiple inheritance, better use the classic way:

class B(A):
def __init__(self, something):
A.__init__(self)
self.y = something

IMHO, you could also benefit from looking at an OO python tutorial
since this is a standard approach.

Regards,
Björn

--
BOFH excuse #145:

Flat tire on station wagon with tapes. ("Never underestimate the
bandwidth of a station wagon full of tapes hurling down the
highway" Andrew S. Tannenbaum)

Sep 10 '07 #5
Nagarajan a écrit :
(snip)
What's the difference b/w:
class A:
and
class A ( object ):
The first one creates a 'classic' (aka 'old-style') class, IOW a class
using the legacy object-model of Python < 2.2. The second one creates a
'new-style' class using the new (well... since Python 2.2, which is not
that new) and far more powerfull object model. Unless you need to
support antique Python version, there's no good reason IMHO to use
old-style classes.
Sep 10 '07 #6

This thread has been closed and replies have been disabled. Please start a new discussion.

Similar topics

4
by: WebRod | last post by:
Hi everybody, I just would like to download the new mails I received on my POP3 server (and keep a copy on the server) I use PHP 4.3 and IMAP functions (they are compatible with both POP3 and...
3
by: Edward Diener | last post by:
In Managed C++ one could use __super to invoke a base class function. Has this been changed to "super" or is it still "__super" ? Looking through the MSDN help I could not find a hit for just...
11
by: Joseph S. | last post by:
Hi all, how do I avoid typing the keyword "$this->" every time I need to reference a member of a class inside the class? (coming from a world of cozy auto-complete enabled Java / .Net IDEs I...
30
by: kj | last post by:
My book (Flanagan's JavaScript: The Definitive Guide, 5th ed.) implies on page 111 that the following two constructs are equivalent: ( x.constructor == Foo ) and ( x instanceof Foo ) The...
0
by: lllomh | last post by:
Define the method first this.state = { buttonBackgroundColor: 'green', isBlinking: false, // A new status is added to identify whether the button is blinking or not } autoStart=()=>{
0
tracyyun
by: tracyyun | last post by:
Hello everyone, I have a question and would like some advice on network connectivity. I have one computer connected to my router via WiFi, but I have two other computers that I want to be able to...
2
by: giovanniandrean | last post by:
The energy model is structured as follows and uses excel sheets to give input data: 1-Utility.py contains all the functions needed to calculate the variables and other minor things (mentions...
4
NeoPa
by: NeoPa | last post by:
Hello everyone. I find myself stuck trying to find the VBA way to get Access to create a PDF of the currently-selected (and open) object (Form or Report). I know it can be done by selecting :...
3
NeoPa
by: NeoPa | last post by:
Introduction For this article I'll be using a very simple database which has Form (clsForm) & Report (clsReport) classes that simply handle making the calling Form invisible until the Form, or all...
1
by: Teri B | last post by:
Hi, I have created a sub-form Roles. In my course form the user selects the roles assigned to the course. 0ne-to-many. One course many roles. Then I created a report based on the Course form and...
0
isladogs
by: isladogs | last post by:
The next Access Europe meeting will be on Wednesday 1 Nov 2023 starting at 18:00 UK time (6PM UTC) and finishing at about 19:15 (7.15PM) Please note that the UK and Europe revert to winter time on...
3
by: nia12 | last post by:
Hi there, I am very new to Access so apologies if any of this is obvious/not clear. I am creating a data collection tool for health care employees to complete. It consists of a number of...
0
isladogs
by: isladogs | last post by:
The next online meeting of the Access Europe User Group will be on Wednesday 6 Dec 2023 starting at 18:00 UK time (6PM UTC) and finishing at about 19:15 (7.15PM). In this month's session, Mike...

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.