Reputation: 10470
I am try to use a decorator to do some debugging and learn about decorators and I do not understand why I am getting this error:
File "/Users/red/PycharmProjects/general_purpose_object_factory/music.py", line 26, in __call__
self._instance = SpotifyService(access_code)
TypeError: wrapper() takes 0 positional arguments but 2 were given
Process finished with exit code
Here is my code:
decorators.py
def debug_printer(func):
def wrapper():
print("Hello")
func()
return wrapper
music.py
import object_factory
from decorators import debug_printer
class MusicServiceProvider(object_factory.ObjectFactory):
def get(self, service_id, **kwargs):
return self.create(service_id, **kwargs)
class SpotifyService:
@debug_printer
def __init__(self, access_code):
self._access_code = access_code
def test_connection(self):
print(f'Accessing Spotify with {self._access_code}')
class SpotifyServiceBuilder:
def __init__(self):
print(f"{__class__.__name__}")
self._instance = None
def __call__(self, spotify_client_key, spotify_client_secret, **_ignored):
if not self._instance:
access_code = self.authorize(
spotify_client_key, spotify_client_secret)
self._instance = SpotifyService(access_code) # <<< LINE 26. !!!!!!
return self._instance
def authorize(self, key, secret):
return 'SPOTIFY_ACCESS_CODE'
Can someone please explain what I am doing wrong?
Upvotes: 0
Views: 1732
Reputation: 10470
ok I had to change my decorator to this ...
def debug_printer(func):
def wrapper(*args, **kwargs):
print(f"Calling {func.__qualname__}({args}, {kwargs})")
return func(*args, **kwargs)
return wrapper
Upvotes: 4