#๐ decorator behind the scenes
41 messages ยท Page 1 of 1 (latest)
@abstract frigate
Remember to:
- Ask your Python question, not if you can ask or if there's an expert who can help.
- Show a code sample as text (rather than a screenshot) and the error message, if you've got one.
- Explain what you expect to happen and what actually happens.
:warning: Do not pip install anything that isn't related to your question, especially if asked to over DMs.
would you be able to tell me what happens when running this?
def foo(f):
f()
def bar():
print("Yay!")
foo(bar)
Yes, do you understand why? Do you see how we can pass bar as an argument to the function call?
Ok good, this is important to understand for how decorators work
a decorator is a function that returns a function
def call_twice(f):
def wrapper():
f()
f()
return wrapper
here's a simple example
!e
def call_twice(f):
def wrapper():
f()
f()
return wrapper
@call_twice
def foo():
print("Yay!")
foo()
:white_check_mark: Your 3.13 eval job has completed with return code 0.
001 | Yay!
002 | Yay!
if I decorate any function with @call_twice, it will call it twice instead of once
this decoration is the equivalent of this
foo = call_twice(foo)
@abstract frigate Are you with me still?
Ok, I just need to see what your understanding of basic decorators is first
just a side note here but, u can do a LOT more with decorators than just decorate using functions that returns functions, thats just one of the main uses 
ignoring the setter for a moment, a property decorator is basically the same as my example above
class Human:
def __init__(self, firstName, lastName):
self.firstName = firstName
self.lastName = lastName
def full_name(self):
return self.firstName + ' ' + self.lastName
full_name = property(full_name)
we're just wrapping property() around our method name
!e
class Human:
def __init__(self, firstName, lastName):
self.firstName = firstName
self.lastName = lastName
def full_name(self):
return self.firstName + ' ' + self.lastName
full_name = property(full_name)
my_human = Human('John', 'Smith')
print(my_human.full_name)
:white_check_mark: Your 3.13 eval job has completed with return code 0.
John Smith
@property does, yes
when we create a property, we can give it a getter, a setter, and a deleter
the decorator syntax is smart and handles methods with duplicate names
but if we didn't want to use decorators, we'd need unique names
yes, it has a setter method
!e
from re import fullmatch
class Human:
def __init__(self, firstName, lastName):
self.firstName = firstName
self.lastName = lastName
def full_name(self):
return self.firstName + ' ' + self.lastName
def set_full_name(self, value):
self.firstName, self.lastName = value.split()
full_name = property(full_name, set_full_name)
my_human = Human('John', 'Smith')
print(my_human.full_name)
my_human.full_name = 'Jane Doe'
print(my_human.full_name)
:white_check_mark: Your 3.13 eval job has completed with return code 0.
001 | John Smith
002 | Jane Doe
just one more thing about decorators, u can stack them 
!e
@print
@lambda func: func()
def watever():
return 4
:white_check_mark: Your 3.13 eval job has completed with return code 0.
4
the first decorator calls the function then the 2nd decorator gets result from that and uses it
thats why u end up with printing 4
that code is essentially the same as print(watever())
:warning: The owner of this post is no longer in the server.
huh
This help channel has been closed. Feel free to create a new post in #1035199133436354600. To maximize your chances of getting a response, check out this guide on asking good questions.