473,320 Members | 1,713 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,320 software developers and data experts.

Decorating instance methods

Hello everybody,

I'm very interesting in using the decorator concept, but I can't
convert it in useful things. I have read many about decorators and
have seen a lot of examples, but I search a possibility, to decorate
methods of classes with reference to the instances. For example:

I have a class A:

class A(object):
def __init__(self, name):
self.name=name

@logging
def hello(self, name):
print 'Hello World.'
>>>a=A('Ernie')
b=A('Bert')
a.hello()
Entering a method. [Ernie]
Hello World.
>>>b.hello()
Entering a method. [Bert]
Hello World.
How should I implement the function logging, when I want to use the
variable self.name for the logging message?

Alex
Jul 9 '07 #1
3 2286
Alexander Draeger schrieb:
Hello everybody,

I'm very interesting in using the decorator concept, but I can't
convert it in useful things. I have read many about decorators and
have seen a lot of examples, but I search a possibility, to decorate
methods of classes with reference to the instances. For example:

I have a class A:

class A(object):
def __init__(self, name):
self.name=name

@logging
def hello(self, name):
print 'Hello World.'
>>>a=A('Ernie')
>>>b=A('Bert')
>>>a.hello()
Entering a method. [Ernie]
Hello World.
>>>b.hello()
Entering a method. [Bert]
Hello World.
How should I implement the function logging, when I want to use the
variable self.name for the logging message?

def logging(m):
def _w(self, *args, **kwargs):
print "name:", self.name
returm m(self, *args, **kwargs)
return _w
Diez
Jul 9 '07 #2
Alexander Draeger <dr**@informatik.tu-chemnitz.dewrote:
Hello everybody,

I'm very interesting in using the decorator concept, but I can't
convert it in useful things. I have read many about decorators and
have seen a lot of examples, but I search a possibility, to decorate
methods of classes with reference to the instances. For example:

I have a class A:

class A(object):
def __init__(self, name):
self.name=name

@logging
def hello(self, name):
print 'Hello World.'
>>a=A('Ernie')
b=A('Bert')
a.hello()
Entering a method. [Ernie]
Hello World.
>>b.hello()
Entering a method. [Bert]
Hello World.
How should I implement the function logging, when I want to use the
variable self.name for the logging message?
(Did you intend that other parameter to the hello method? If so you had
better pass it in when you call the method.)

Try this:
>>def logging(f):
def deco(self, *args, **kw):
print "Entering a method [%s]" % self.name
return f(self, *args, **kw)
return deco
>>class A(object):
def __init__(self, name):
self.name=name

@logging
def hello(self, name):
print 'Hello World.'

>>a=A('Ernie')
a.hello('world')
Entering a method [Ernie]
Hello World.
Jul 9 '07 #3
....
Try this:
....
Sesame __Street__ Version ....

'''
NewsGroup .... comp.lang.python
Subject ...... Decorating instance methods
Post_By ...... Alexander Draeger
Reply_By ..... Duncan Booth
Edit_By ...... Stanley C. Kitching
'''

def logging( f ) :

def deco( self , *args , **kw ) :

print " %s in da house !" % self.name
return f( self , *args , **kw )

return deco
class A( object ) :

def __init__( self , name ) :
self.name = name

@logging
def hello( self ) :
print ' Yo, %s .... \n' % self.name
def test_01() :

print

list_names = [ 'Bert' , 'Cookie Monster' ,
'Count Dracula' , 'Ernie' ]

list_objects = [ A( this_name ) for this_name in list_names ]

for this_object in list_objects :
this_object.hello()
if __name__ == '__main__' :

test_01()
--
Stanley C. Kitching
Human Being
Phoenix, Arizona
----== Posted via Newsfeeds.Com - Unlimited-Unrestricted-Secure Usenet News==----
http://www.newsfeeds.com The #1 Newsgroup Service in the World! 120,000+ Newsgroups
----= East and West-Coast Server Farms - Total Privacy via Encryption =----
Jul 9 '07 #4

This thread has been closed and replies have been disabled. Please start a new discussion.

Similar topics

6
by: yyy | last post by:
my question is rather theoretical than practical in the pure programming aspect... is "name mangling" the same as "name decorating" ? browsing the web, you might find multiple people saying...
12
by: Andy Terrel | last post by:
Okay does anyone know how to decorate class member functions? The following code gives me an error: Traceback (most recent call last): File "decorators2.py", line 33, in <module> s.update()...
1
by: hardieca | last post by:
Hi! I decorate my unfinished classes and methods with a custom TODO attribute (as in things To Do). Using reflection, I am then able to parse through my classes and output all TODOs to a list I...
1
by: 1x7y2z9 | last post by:
Say, we have a (parent) class P. It has N child classes C1(P), C2(P) ... CN(P) Each of the child classes defines (differently) a method func(). I wish to decorate all of the CX.func() in the...
0
by: DolphinDB | last post by:
Tired of spending countless mintues downsampling your data? Look no further! In this article, you’ll learn how to efficiently downsample 6.48 billion high-frequency records to 61 million...
0
by: ryjfgjl | last post by:
ExcelToDatabase: batch import excel into database automatically...
1
isladogs
by: isladogs | last post by:
The next Access Europe meeting will be on Wednesday 6 Mar 2024 starting at 18:00 UK time (6PM UTC) and finishing at about 19:15 (7.15PM). In this month's session, we are pleased to welcome back...
0
by: jfyes | last post by:
As a hardware engineer, after seeing that CEIWEI recently released a new tool for Modbus RTU Over TCP/UDP filtering and monitoring, I actively went to its official website to take a look. It turned...
0
by: ArrayDB | last post by:
The error message I've encountered is; ERROR:root:Error generating model response: exception: access violation writing 0x0000000000005140, which seems to be indicative of an access violation...
1
by: Defcon1945 | last post by:
I'm trying to learn Python using Pycharm but import shutil doesn't work
1
by: Shællîpôpï 09 | last post by:
If u are using a keypad phone, how do u turn on JavaScript, to access features like WhatsApp, Facebook, Instagram....
0
by: Faith0G | last post by:
I am starting a new it consulting business and it's been a while since I setup a new website. Is wordpress still the best web based software for hosting a 5 page website? The webpages will be...
0
isladogs
by: isladogs | last post by:
The next Access Europe User Group meeting will be on Wednesday 3 Apr 2024 starting at 18:00 UK time (6PM UTC+1) and finishing by 19:30 (7.30PM). In this session, we are pleased to welcome former...

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.