473,395 Members | 1,797 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 473,395 software developers and data experts.

manipulating class attributes from a decorator while the class isbeing defined

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
4 1597
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
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
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
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 thread has been closed and replies have been disabled. Please start a new discussion.

Similar topics

12
by: Humpty Dumpty | last post by:
Hello, I'm experimenting with different ways of extending a class (for a plug-ins framework for a GUI) with more than one extension when some of these extensions need to collaborate, but others...
5
by: Neal | last post by:
Hi I need to change my mousepointer when the mouse moves over certain populated controls on my aspx page. I've tried the following but without success (after visiting a site recommended from...
2
by: lcaamano | last post by:
We have a tracing decorator that automatically logs enter/exits to/from functions and methods and it also figures out by itself the function call arguments values and the class or module the...
0
by: emin.shopper | last post by:
I had a need recently to check if my subclasses properly implemented the desired interface and wished that I could use something like an abstract base class in python. After reading up on metaclass...
6
by: Chris Fonnesbeck | last post by:
I have a class that does MCMC sampling (Python 2.5) that uses decorators -- one in particular called _add_to_post that appends the output of the decorated method to a class attribute. However, when...
0
by: Christian Heimes | last post by:
Wilbert Berendsen schrieb: 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....
0
by: Karl-Heinz Ruskowski | last post by:
How can I reach the class attribute `regexps' from within a decorator? Now, the first way that comes to my mind is simply overloading the class and set your regexps variable in your new class. ...
13
by: Hussein B | last post by:
Hi, I'm familiar with static method concept, but what is the class method? how it does differ from static method? when to use it? -- class M: def method(cls, x): pass method =...
11
by: Rafe | last post by:
Hi, I'm working within an application (making a lot of wrappers), but the application is not case sensitive. For example, Typing obj.name, obj.Name, or even object.naMe is all fine (as far as...
0
by: Charles Arthur | last post by:
How do i turn on java script on a villaon, callus and itel keypad mobile phone
0
by: ryjfgjl | last post by:
In our work, we often receive Excel tables with data in the same format. If we want to analyze these data, it can be difficult to analyze them because the data is spread across multiple Excel files...
0
by: emmanuelkatto | last post by:
Hi All, I am Emmanuel katto from Uganda. I want to ask what challenges you've faced while migrating a website to cloud. Please let me know. Thanks! Emmanuel
0
BarryA
by: BarryA | last post by:
What are the essential steps and strategies outlined in the Data Structures and Algorithms (DSA) roadmap for aspiring data scientists? How can individuals effectively utilize this roadmap to progress...
1
by: nemocccc | last post by:
hello, everyone, I want to develop a software for my android phone for daily needs, any suggestions?
0
by: Hystou | last post by:
Most computers default to English, but sometimes we require a different language, especially when relocating. Forgot to request a specific language before your computer shipped? No problem! You can...
0
Oralloy
by: Oralloy | last post by:
Hello folks, I am unable to find appropriate documentation on the type promotion of bit-fields when using the generalised comparison operator "<=>". The problem is that using the GNU compilers,...
0
jinu1996
by: jinu1996 | last post by:
In today's digital age, having a compelling online presence is paramount for businesses aiming to thrive in a competitive landscape. At the heart of this digital strategy lies an intricately woven...
0
by: Hystou | last post by:
Overview: Windows 11 and 10 have less user interface control over operating system update behaviour than previous versions of Windows. In Windows 11 and 10, there is no way to turn off the Windows...

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.