471,354 Members | 1,472 Online
Bytes | Software Development & Data Engineering Community
Post +

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 471,354 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 1287
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 discussion thread is closed

Replies have been disabled for this discussion.

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.