#๐Ÿ”’ decorator behind the scenes

41 messages ยท Page 1 of 1 (latest)

robust remnantBOT
#

@abstract frigate

Python help channel opened

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.

ivory rock
#

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

robust remnantBOT
ivory rock
#

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

tulip wraith
ivory rock
#

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)
robust remnantBOT
ivory rock
#

@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

tulip wraith
#

indeed

#
>>> type(property())
<class 'property'>
>>> 
ivory rock
#

!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)
robust remnantBOT
ivory rock
#

not a decorator in sight

#

idk how that re import got in there, haha

tulip wraith
#

just one more thing about decorators, u can stack them pithink

#

!e

@print
@lambda func: func()
def watever():
    return 4
robust remnantBOT
tulip wraith
#

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

robust remnantBOT
#

:warning: The owner of this post is no longer in the server.

tulip wraith
#

huh

robust remnantBOT
#
Python help channel closed

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.