Skip to content

Instantly share code, notes, and snippets.

@pielgrzym
Created November 13, 2011 10:02
Show Gist options
  • Save pielgrzym/1361933 to your computer and use it in GitHub Desktop.
Save pielgrzym/1361933 to your computer and use it in GitHub Desktop.
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