SFML community forums

Bindings - other languages => Python => Topic started by: shantytown on September 21, 2014, 11:02:00 pm

Title: Question on inheritance
Post by: shantytown 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.
Title: Re: Question on inheritance
Post by: rojohound 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 (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 (https://github.com/Sonkun/python-sfml)
Title: Re: Question on inheritance
Post by: shantytown 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 (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 (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.