Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Ability to remove a coin #2

Open
alxwrd opened this issue Sep 20, 2018 · 2 comments
Open

Ability to remove a coin #2

alxwrd opened this issue Sep 20, 2018 · 2 comments

Comments

@alxwrd
Copy link
Owner

alxwrd commented Sep 20, 2018

Coins can be created by subclassing Coin, but once created they can't be removed.

See: https://stackoverflow.com/q/52428679/7220776

@alxwrd
Copy link
Owner Author

alxwrd commented Sep 21, 2018

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.

@alxwrd
Copy link
Owner Author

alxwrd commented Oct 4, 2018

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)

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

1 participant