heyo... i'm learning decorators. interesting stuff. i think i'm missing something though, or maybe it's not the decorators but how i'm working with classes? when I do this in terminal it works:
class Client:
"""The main client."""
def __init__(self, config, creds):
"""Initialize."""
self.config = config
self.creds = creds
def context(func, *args, **kwargs):
@wraps(func)
def func_with_context(*args, **kwargs):
print(f'contextualizing {func.__name__}')
print(creds.user_name)
return (args, kwargs)
return func_with_context
@context
def test(self, creds):
pass
i have two package objects i can instantiate and pass in:
creds = mypackage.credentials.auto()
config = mypackage.Configuration()
and then when i do:
client = Client(config, creds)
client.test('yo')
>>>contextualizing test
username@org
((<__main__.Client object at 0x101b66208>, 'yo'), {})
but when i do this from the package itself i get
import mypackage as mypackage
client = mypackage.Client(config, creds)
client.test('yo')
contextualizing test
Traceback (most recent call last):
File "<stdin>", line 1, in <module>
File "/Users/dblack/Code/mypackage/mypackage/client.py", line 51, in func_with_context
creds.user_name,
NameError: name 'creds' is not defined
why would this work when i just declare a class but not when it's in the package namespace?