Click here to Skip to main content
15,886,110 members
Please Sign up or sign in to vote.
0.00/5 (No votes)
See more:
Hello All,

I was having a look at class decorators. Basically i implemented a class decorated by making it callable.

I added some print statement in the __call__ method. I observed that the print statement gets executed twice.

My doubt was that are magic methods invoked the moment you instantiate a class? Or do we wait for an explicit action which would trigger these methods.

The latter is true, but i am confused with automatic invocation of magic methods

What I have tried:

>>> class decorator:
	def __init__(self,f):
		self.f=f
	def __call__(self):
		print ('decorated')
		self.f()

		
>>> @decorator
def func():
	print('Hi')

	
>>> func()
decorated
Hi
>>> d = decorator(func)
>>> d()
decorated
decorated
Hi


As you can see, the string "decorated" is repeated twice. However, as per my understanding the output should have only "decorated" string once and one string from the decorated function.

I would request suggestions/inputs.

Thanks and Regards,
Rahul
Posted
Updated 10-Jun-19 22:57pm

1 solution

The following program
Python
class decorator:
  def __init__(self,f):
    self.f=f
  def __call__(self):
    print ('decorated')
    self.f()

def func():
  print('Hi')


func()
print("-----------------")
d = decorator(func)
d()

outputs
Hi
-----------------
decorated
Hi

I see nothing strange such an output.
 
Share this answer
 
v2
Comments
Rahul VB 10-Jun-19 5:18am    
Thanks for trying. But i am sure that i saw the prints twice and i copy pasted the output from the interpreter (idle). Let me try the way you did and cross check. Thanks for trying out :)
Richard MacCutchan 10-Jun-19 6:40am    
I'm not sure that I fully understand decorators in Python. But I assume that func is not part of the decorator class, but gets executed as a result of the self.f() command. Also, why do you not need the @... meta statements for it?
Rahul VB 11-Jun-19 4:53am    
The @ statement is called decoration:
For ex:
def decorator(func):
def wrapper():
additional code...
func()
return wrapper

def originalFunc():
print('i am the original func')

modifiedFunc = decorator(originalFunc)
modifiedFunc()
- So when i call modifiedFunc it gets called with additional code inside the wrapper along with the original functionality.

Instead of doing all the above i can simply do:

@decorator
def originalFunc():
print('i am the original func')

Now when i call originalFunc(), it work the way exactly i showed above.
This is a very important functionality in python. A simple function can behave as per requirement differently along with its original code depending upon requirements.
So, basically my question was on class decorators.

Thanks,
Rahul
Richard MacCutchan 11-Jun-19 5:33am    
Something I need to study more ...

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900