Welcome, Guest. Please login or register. Did you miss your activation email?

Author Topic: Question on inheritance  (Read 6505 times)

0 Members and 1 Guest are viewing this topic.

shantytown

  • Newbie
  • *
  • Posts: 2
    • View Profile
Question on inheritance
« on: September 21, 2014, 11:02:00 pm »
class MyRectangleShape(sfml.graphics.RectangleShape):
        def __init__(self):
                print('Output: ' + str(self.size))

box = MyRectangleShape()

Code: [Select]
Output: 0.0x, 0.0y
Why is RectangleShape's constructor implicitly called here? self should not have the attribute size without explicitly calling RectangleShape.__init__(). The reason this is significant is because this will cause all kinds of problems with the way I want to expand this class. Python isn't supposed to work that way. The only way I can imagine this would be intentional is if there is some metaprogramming causing it, and I don't know much about that.

Tried in Python 2.7.7 and 3.2.5. Same behavior in both.

Until I understand the problem, I guess I'll just stick to composition.
« Last Edit: September 22, 2014, 12:31:13 am by shantytown »

rojohound

  • Newbie
  • *
  • Posts: 8
    • View Profile
Re: Question on inheritance
« Reply #1 on: September 22, 2014, 03:45:25 am »
Best I can tell cython calls __cinit__ of the parent class first before __init__ of the inheriting class.
http://stackoverflow.com/questions/18260095/cant-override-init-of-class-from-cython-extension

In the latest from github __init__ is used instead of __cinit__ so I imagine inheritance should work there.  I can't find exactly when it changed though.
https://github.com/Sonkun/python-sfml

shantytown

  • Newbie
  • *
  • Posts: 2
    • View Profile
Re: Question on inheritance
« Reply #2 on: September 22, 2014, 03:49:19 pm »
Best I can tell cython calls __cinit__ of the parent class first before __init__ of the inheriting class.
http://stackoverflow.com/questions/18260095/cant-override-init-of-class-from-cython-extension

In the latest from github __init__ is used instead of __cinit__ so I imagine inheritance should work there.  I can't find exactly when it changed though.
https://github.com/Sonkun/python-sfml

Perfect. That's exactly the answer I was looking for. I did some googling myself but wasn't looking for the right information I guess. Thanks a lot for your help.