Decorating instance methods

  • Thread starter Alexander Draeger
  • Start date
A

Alexander Draeger

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.'


Entering a method. [Ernie]
Hello World.
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
 
D

Diez B. Roggisch

Alexander said:
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.'


Entering a method. [Ernie]
Hello World.
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
 
D

Duncan Booth

Alexander Draeger said:
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.'


Entering a method. [Ernie]
Hello World.
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 deco(self, *args, **kw):
print "Entering a method [%s]" % self.name
return f(self, *args, **kw)
return deco
def __init__(self, name):
self.name=name

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

Entering a method [Ernie]
Hello World.
 
C

Cousin Stanley

....
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()
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

Forum statistics

Threads
473,968
Messages
2,570,154
Members
46,702
Latest member
LukasConde

Latest Threads

Top