newb __init__ inheritance

hyperboogie hyperboogie at gmail.com
Sun Mar 11 06:18:26 EDT 2012


On Mar 11, 12:47 am, "Colin J. Williams" <c... at ncf.ca> wrote:
> On 10/03/2012 12:58 PM, Colin J. Williams wrote:> On 08/03/2012 10:25 AM, hyperboogie wrote:
> >> Hello everyone.
>
> [snip]
> > main()
> > I'm not sure that the class initialization is required.
>
> > Good luck,
>
> > Colin W.
>
> When I wrote earlier, I wondered about the need for initialization.
>
> With Version 2, both __new__ and __init__ were required, not in the
> example below, using version 3.2:
> #!/usr/bin/env python
>
> class A():
>
>    def ringA(self):
>      print ('aaa')
>
>    def ringB(self):
>      print('bbb')
>
> class B(A):
>    def __init__(self:)
>    def ringB(self):
>      print('BBB')
>
> a= A()
> b= B()
> b.ringB()
> b.ringA()
> b.__class__.mro()[0].ringB(22)   #  22 is used for the ringB attribute
>                                   #  Trial and error shows that any
>                                   #  non-Null,including None for the
>                                   #  argument gives the same result
> z= 1
> def main():
>      pass
>
> if __name__ == '__main__':
>      main()
>
> Colin W.

thank you everyone...
Still things are not working as expected... what am I doing wrong?
I'm working with python2 and have the following issues:

1. mro is not an attribute/function
2. inheritance is not working as expected:

# cat test.py
#!/usr/bin/python

class A():
   def __init__(self):
      z=1
      print "in A.__init__ z=", z

   def funcA(self):
      print "in funcA - class A"

   def funcB(self):
      print "in funcB - class A, z= ", z

class B(A):
   def __init__(self):
      A.__init__(self)
      print "in B.__init__ z=", z

   def funcB(self):
      print "in funcB - class B, z= ", z

a=A()
b=B()
b.funcB()
b.funcA()

# ./test.py
in A.__init__ z= 1    # This must be the __init__ from the
instantiation of a
in A.__init__ z= 1    # This must be the B.__init__ calling A.__init__
in B.__init__ z=      # Why isn't this working? z should have been
inherited from "A" right?
Traceback (most recent call last):
  File "./test.py", line 23, in <module>
    b=B()
  File "./test.py", line 17, in __init__
    print "in B.__init__ z=", z
NameError: global name 'z' is not defined
#



More information about the Python-list mailing list