473,768 Members | 7,620 Online
Bytes | Software Development & Data Engineering Community
+ Post

Home Posts Topics Members FAQ

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 1402
Nagarajan <na****@gmail.c omwrote:
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...@i nvalid.invalidw rote:
Nagarajan <nag...@gmail.c omwrote:
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...@i nvalid.invalidw rote:
Nagarajan <nag...@gmail.c omwrote:
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
1982
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 IMAP servers). When I use OUTLOOK, no problem, in less than 5 seconds, it answers me: " you have XX new messages"
3
5977
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 "super".
11
3082
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 find it a bit annoying) TIA, JS
30
4701
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 author states that the instanceof operator computes its result
0
9576
marktang
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...
1
9961
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,...
0
9843
tracyyun
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...
0
8840
agi2029
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...
1
7384
isladogs
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...
0
5283
by: TSSRALBI | last post by:
Hello I'm a network technician in training and I need your help. I am currently learning how to create and manage the different types of VPNs and I have a question about LAN-to-LAN VPNs. The last exercise I practiced was to create a LAN-to-LAN VPN between two Pfsense firewalls, by using IPSEC protocols. I succeeded, with both firewalls in the same network. But I'm wondering if it's possible to do the same thing, with 2 Pfsense firewalls...
0
5425
by: adsilva | last post by:
A Windows Forms form does not have the event Unload, like VB6. What one acts like?
1
3932
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
2
3534
muto222
by: muto222 | last post by:
How can i add a mobile payment intergratation into php mysql website.

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.