Defining class attributes + inheritance
Santoso Wijaya
santoso.wijaya at gmail.com
Tue Mar 8 20:23:42 EST 2011
Here's how you do inheritance:
C:\>python
Python 2.7.1 (r271:86832, Nov 27 2010, 17:19:03) [MSC v.1500 64 bit (AMD64)]
on
win32
Type "help", "copyright", "credits" or "license" for more information.
>>> class BaseClass(object):
... def __init__(self, a, b, c, d):
... self.a = a
... self.b = b
... self.c = c
... self.d = d
...
>>> class NewClass(BaseClass):
... def __init__(self, a, b, c, d, new):
... super(NewClass, self).__init__(a, b, c, d)
... self.new = new
...
>>> A = BaseClass(1,2,3,4)
>>> print A.a, A.b, A.c, A.d
1 2 3 4
>>> B = NewClass(1,2,3,4,5)
>>> print B.a, B.b, B.c, B.d, B.new
1 2 3 4 5
>>>
~/santa
On Tue, Mar 8, 2011 at 5:00 PM, Martin De Kauwe <mdekauwe at gmail.com> wrote:
> On Mar 9, 11:50 am, "Rhodri James" <rho... at wildebst.demon.co.uk>
> wrote:
> > On Wed, 09 Mar 2011 00:29:18 -0000, Martin De Kauwe <mdeka... at gmail.com>
>
> > wrote:
> >
> >
> >
> >
> >
> >
> >
> > > On Mar 9, 10:20 am, Ethan Furman <et... at stoneleaf.us> wrote:
> > [snip]
> > >> Just make sure and call the parent's constructor, either with
> >
> > >> class NewClass(BaseClass):
> > >> def __init__(self, ....):
> > >> BaseClass.__init__(self, other_params)
> >
> > >> or
> >
> > >> class NewClass(BaseClass):
> > >> def __init__(self, ....):
> > >> super(NewClass, self).__init__(....)
> >
> > >> ~Ethan~
> >
> > > Hi thanks, but I think I am implementing it wrong then?
> >
> > > BaseClass has 4 attributes and when I tried what you said
> >
> > > class NewClass(BaseClass):
> > > def __init__(self):
> > > super(NewClass, self).__init__(new_thing)
> >
> > > I get the error
> >
> > > TypeError: __init__() takes exactly 1 argument (6 given)
> >
> > Please give us either the rest of the code or the rest of the
> > traceback, or preferably both. Without one or the other we have
> > little hope of guessing what you've typed.
> >
> > --
> > Rhodri James *-* Wildebeest Herder to the Masses
>
> OK
>
> class BaseClass(object):
>
> def __init__(self, a, b, c, d):
> self.a = a
> self.b = b
> self.c = c
> self.d = d
>
>
> class NewClass(BaseClass):
> def __init__(self):
> super(NewClass, self).__init__(new)
> self.new = new
> print self.new
>
> class PreviousClass:
> def __init__(self, a, b, c, d, new):
> self.a = a
> self.b = b
> self.c = c
> self.d = d
> self.new = new
> print self.new
>
>
> if __name__ == "__main__":
>
> A = PreviousClass(1, 2, 3, 4, 5)
> B = NewClass(1, 2, 3, 4, 5)
>
> $ python test.py
> Traceback (most recent call last):
> File "model_data.py", line 29, in <module>
> B = NewClass(1, 2, 3, 4, 5)
> TypeError: __init__() takes exactly 1 argument (6 given)
>
>
>
> So NewClass is my attempt to implement what I was shown and
> PreviousClass was how I was originally solving the issue, i.e. I
> wouldn't inherit the BaseClass.
>
> thanks
> --
> http://mail.python.org/mailman/listinfo/python-list
>
-------------- next part --------------
An HTML attachment was scrubbed...
URL: <http://mail.python.org/pipermail/python-list/attachments/20110308/78dd31ab/attachment-0001.html>
More information about the Python-list
mailing list