Hacker Newsnew | past | comments | ask | show | jobs | submit | nurettin's favoriteslogin

In Python, we use mixins. Mixins can only inherit from 'object' and nothing else, like this:

    class PhysicsobjectMixin(object):
        def update_physics(self):
            pass
        
        def apply_konckback(self, force):
            pass

        def get_position(self):
            pass


    class FightMixin(object):
        def attack(self):
            pass

        def defend(self):
            pass

    
    class TalkMixin(object):
        def say_something(self):
            pass


    class Character(PhysicsobjectMixin, FightMixin, TalkMixin):
        pass


    class Pickup(PhysicsobjectMixin):
        pass


    class Projectile(PhysicsobjectMixin):
        pass

it's still inheritance, but the classes will be flat; every class only inherits one deep, so there will be no diamond problems and no repeating code.

Consider applying for YC's Winter 2026 batch! Applications are open till Nov 10

Guidelines | FAQ | Lists | API | Security | Legal | Apply to YC | Contact

Search: