Velocity Reviews - Computer Hardware Reviews

Velocity Reviews > Newsgroups > Programming > Python > Scope of decorator argument

Reply
Thread Tools

Scope of decorator argument

 
 
Gregor Horvath
Guest
Posts: n/a
 
      10-17-2006
Hi,

this:

class base(object):
@adecorator(xy)
def edit(self):
print "edit"

class child(base):
xy = 3


obviously fails because "xy" is not bound at class creation time of the
base object.

One solution could be delegation:

class base(object):
@classmethod
def edit(self):
print "do the real work here"

class child(object):
xy = 3
mybase = base

@adecorator(xy)
def edit(self, *args, **kwargs):
self.mybase.edit(*args, **kwargs)

But then I have the ugly boiler plate delegation code in child.

Is there any other solution, probably with metaclasses ?

--
Servus, Gregor
 
Reply With Quote
 
 
 
 
Gregor Horvath
Guest
Posts: n/a
 
      10-17-2006
Gregor Horvath schrieb:
>
> Is there any other solution, probably with metaclasses ?
>


I've found this one:

class mymeta(type):
def __new__(meta, class_name, bases, new_attrs):
new_attrs["edit"] = adecorator(new_attrs['xy'])(bases[0].edit))
return type.__new__(meta, class_name, bases, new_attrs)

class base(object):
def edit(self):
print "%s edit" % self

class child(base):
__metaclass__ = mymeta
xy = 3


Don't know if it's the best solution but it seems to work.

--
Servus, Gregor

 
Reply With Quote
 
 
 
Reply

Thread Tools

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is On
HTML code is Off
Trackbacks are On
Pingbacks are On
Refbacks are Off


Similar Threads
Thread Thread Starter Forum Replies Last Post
Class decorator with argument mk Python 0 01-16-2009 08:15 PM
Why doesnt __getattr__ with decorator dont call __get_method in decorator glomde Python 5 03-29-2007 02:48 PM
Decorator for Enforcing Argument Types Chris Python 20 12-23-2006 01:38 PM
Scope - do I need two identical classes, each with different scope? ann Java 13 09-13-2005 03:07 AM
IMPORT STATIC; Why is "import static" file scope? Why not class scope? Paul Opal Java 12 10-10-2004 11:01 PM



Advertisments