Giter VIP home page Giter VIP logo

Comments (2)

alxwrd avatar alxwrd commented on July 18, 2024

I like the comment from @PatrickHugh:

Instead of manipulating __subclasses__, I would recommend keeping your own list instead, possible through the __init_subclass__ hook

class Coin:
    subcoins = []

    def __init_subclass__(cls):
        Coin.subcoins.append(cls)

    @classmethod
    def remove_subcoin(cls, coin):
        Coin.subcoins.remove(cls)

Then instead of Coin.__subclasses__(), use Coin.subcoins.

from coinhandler.

alxwrd avatar alxwrd commented on July 18, 2024

Coin already has a method for getting sub coins, added in 8b7aa0e: Coin.sub_coins().

Might be best to mark the list tracking the coins private.

__init_subclass__ should also ensure the coin isn't already in _subcoins. So maybe this should be a set?

class Coin:
    _subcoins = set()

    def __init_subclass__(cls):
        Coin._subcoins.add(cls)

    @classmethod
    def remove_subcoin(cls, coin):
        Coin._subcoins.remove(cls)

from coinhandler.

Related Issues (4)

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    🖖 Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo D3

    Bring data to life with SVG, Canvas and HTML. 📊📈🎉

Recommend Topics

  • javascript

    JavaScript (JS) is a lightweight interpreted programming language with first-class functions.

  • web

    Some thing interesting about web. New door for the world.

  • server

    A server is a program made to process requests and deliver data to clients.

  • Machine learning

    Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google ❤️ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.