[Python-3000] PEP Draft: Class Decorators (original) (raw)
Brett Cannon brett at python.org
Sat Mar 10 03:23:51 CET 2007
- Previous message: [Python-3000] PEP Draft: Class Decorators
- Next message: [Python-3000] Discussions with no PEPs
- Messages sorted by: [ date ] [ thread ] [ subject ] [ author ]
[SNIP]
class FormFactory(Form): idtoform = {} # { accountid : { formid : formclass } } @staticmethod def register(cls, accountid, formid): FormFactory.idtoform.setdefault(cls.accountid, {}) assert formid not in FormFactory.idtoform[cls.accountid], formid FormFactory.idtoform[cls.accountid][formid] = cls # return the class unchanged return cls
@FormFactory.register(accountid=17, formid=3) class FormCaptureA(object): # .. cgi param to sql mapping defined, etc
I don't think this example works. You have a method call that is lacking its first argument (did you meant to have it be a classmethod?). Plus that call itself will just return FormFactory for the decorator which itself does not define call and thus will raise an exception when the decorator is called with FormCapture. I think you meant something like the following::
class FormFactory(Form): id_to_form = {} # { account_id : { form_id : form_class } }
@classmethod def register(cls, account_id, form_id): def inner(to_decorate): FormFactory.id_to_form.setdefault(cls.account_id, {}) assert form_id not in FormFactory.id_to_form[cls.account_id], form_id FormFactory.id_to_form[cls.account_id][form_id] = cls # return the class unchanged return to_decorate return inner
Otherwise define a call method on FormFactory or use functools.partial to create the closure for partial function application for the arguments you want.
And yes, Jack, you can check it in yourself once the initial big problems with the PEP have been addressed.
-Brett
- Previous message: [Python-3000] PEP Draft: Class Decorators
- Next message: [Python-3000] Discussions with no PEPs
- Messages sorted by: [ date ] [ thread ] [ subject ] [ author ]