Created
November 13, 2011 10:02
-
-
Save pielgrzym/1361933 to your computer and use it in GitHub Desktop.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
class BasketBase(type): | |
def __new__(mcs, classname, bases, class_dict): | |
class_dict['actions'] = {} | |
class_dict['basketactions'] = {} | |
for elem in class_dict: | |
if elem.startswith("action_"): | |
class_dict['actions'][elem.replace("action_","")] = class_dict[elem] | |
if elem.startswith("basketaction_"): | |
class_dict['basketactions'][elem.replace("basketaction_","")] = class_dict[elem] | |
for elem in class_dict.keys(): | |
if elem.startswith("action_") or elem.startswith("basketaction_"): | |
del class_dict[elem] | |
return type.__new__(mcs, classname, bases, class_dict) | |
class Basket(object): | |
__metaclass__ = BasketBase | |
def action_remove(self): | |
pass | |
# now: | |
# > Basket.actions | |
# {'remove': <function action_remove at 0x1946410>} | |
# but: | |
class MyFavoritesBasket(Basket): | |
def action_blah(self): | |
pass | |
# > MyFavoritesBasket.actions | |
# {'blah': <function action_blah at 0x1c1b2a8>} | |
# why no 'remove' also??? | |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment