By using this site, you agree to our updated Privacy Policy and our Terms of Use. Manage your Cookies Settings.
448,712 Members | 1,591 Online
Bytes IT Community
+ Ask a Question
Need help? Post your question and get tips & solutions from a community of 448,712 IT Pros & Developers. It's quick & easy.

Re: manipulating class attributes from a decorator while the classis being defined

P: n/a
Wilbert Berendsen schrieb:
Hi, is it possible to manipulate class attributes from within a decorator
while the class is being defined?

I want to register methods with some additional values in a class attribute.
But I can't get a decorator to change a class attribute while the class is
still being defined. Something like:

class Parser(object):

regexps = []
def reg(regexp):
def deco(func):
regexps.append((regexp, func))
return func
return deco

@reg(r'".*"')
def quoted_string(self):
pass

How can I reach the class attribute `regexps' from within a decorator?
It's really tricky . The class object doesn't exists yet. It's created
after all functions are parsed and created. You have can walk up the
stack frames but it's ugly.

Christian

Jun 27 '08 #1
Share this question for a faster answer!
Share on Google+

This discussion thread is closed

Replies have been disabled for this discussion.