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

Scope of decorator argument

P: n/a
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
Oct 17 '06 #1
Share this Question
Share on Google+
1 Reply


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

Oct 17 '06 #2

This discussion thread is closed

Replies have been disabled for this discussion.