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

manipulating class attributes from a decorator while the class isbeing defined

P: n/a
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?

Thanks for any help,
Wilbert Berendsen

--
http://www.wilbertberendsen.nl/
"You must be the change you wish to see in the world."
-- Mahatma Gandhi
Jun 27 '08 #1
Share this Question
Share on Google+
4 Replies


P: n/a
On Apr 19, 10:19*pm, Wilbert Berendsen <wbs...@xs4all.nlwrote:
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?
In this particular example, it is enought to change the line

def reg(regexp):

to:

def reg(regexp, regexps=regexps):

So that 'regexps' inside reg() will point to the same object as
'regexps' outside reg().

Of course it wouldn't work well with inheritance, but it seems to me
that you don't want to subclass 'Parser' anyway. If you did, you
could instead have something like this:

class Ruleset(list):

def add(self, regexp):
def decorator(func):
self.append((regexp, func))
return func
return decorator
class Parser(object):

rules = Ruleset()

@rules.add(r'".*"')
def quoted_string(self):
pass
Or, yet another solution is to change the metaclass of 'Parser' so
that it populates the class's rules by scanning its attributes.

HTH

--
Arnaud

Jun 27 '08 #2

P: n/a
Wilbert Berendsen a écrit :
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?
Simple answer : you can't. Because, as you noticed, the class object
doesn't exist yet.

The canonical solutions are either to store regexps outside the class
(ie: as a module level variable) - which can be problematic in some
cases -, or to use a two-pass scheme using a decorator and a metaclass,
where the decorator annotate the function with required informations for
latter processing, and the metaclass do the effective processing.

There are of course other solutions, some of them possibly simpler. The
'best' solution of course depends on intented use of your class...

HTH
Jun 27 '08 #3

P: n/a
Wilbert Berendsen <wb****@xs4all.nlwrote:
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?
Have you tried passing regexps into the decorator as a default argument?

def reg(regexp, regexps=regexps):
def deco(func):
regexps.append((regexp, func))
return func
return deco
Jun 27 '08 #4

P: n/a
On Apr 19, 11:19 pm, Wilbert Berendsen <wbs...@xs4all.nlwrote:
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?

Thanks for any help,
Wilbert Berendsen

--http://www.wilbertberendsen.nl/
"You must be the change you wish to see in the world."
-- Mahatma Gandhi
---------------------------------------------------

def reg(regexp):
def deco(func):
def inner(self, *args, **kw):
if not hasattr(self, 'regexps'):
self.regexps = []
self.regexps.append((regexp, func))
return func(self, *args, **kw)
return inner
return deco

class Parser(object):

regexps = []
@reg(r'".*"')
def quoted_string(self):
print 'hi'

p = Parser()

p.quoted_string()

print p.regexps
---------------------------------------------------
Jun 27 '08 #5

This discussion thread is closed

Replies have been disabled for this discussion.