Defining class attributes + inheritance

James Mills prologic at shortcircuit.net.au
Tue Mar 8 19:53:57 EST 2011


On Wed, Mar 9, 2011 at 9:20 AM, Ethan Furman <ethan at stoneleaf.us> wrote:
> 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__(....)

In Python3 this is even easier (for the simplest case):

>>> class Base:
...     def __init__(self, x):
...             print("Hello %s" % x)
...
>>> class ExtendedBase(Base):
...     def __init__(self, x, y):
...             super().__init__(x)
...             print("Hello %s" % y)
...
>>> x = ExtendedBase("foo", "bar")
Hello foo
Hello bar
>>>

cheers
James

-- 
-- James Mills
--
-- "Problems are solved by method"



More information about the Python-list mailing list