473,387 Members | 1,536 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 473,387 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 1385
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: taylorcarr | last post by:
A Canon printer is a smart device known for being advanced, efficient, and reliable. It is designed for home, office, and hybrid workspace use and can also be used for a variety of purposes. However,...
0
by: Charles Arthur | last post by:
How do i turn on java script on a villaon, callus and itel keypad mobile phone
0
by: emmanuelkatto | last post by:
Hi All, I am Emmanuel katto from Uganda. I want to ask what challenges you've faced while migrating a website to cloud. Please let me know. Thanks! Emmanuel
1
by: nemocccc | last post by:
hello, everyone, I want to develop a software for my android phone for daily needs, any suggestions?
0
by: Hystou | last post by:
There are some requirements for setting up RAID: 1. The motherboard and BIOS support RAID configuration. 2. The motherboard has 2 or more available SATA protocol SSD/HDD slots (including MSATA, M.2...
0
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,...
0
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...
0
Oralloy
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,...
0
jinu1996
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...

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.