[Tutor] Question about python decorators

Nathan Smith nathan-tech at hotmail.com
Mon Jul 11 17:29:33 EDT 2022


Hiya,


I figured this is how they work, so it's nice to have that confirmed!

For some reason though I have a library that is not behaving this way, 
to example:


@object.attr.method

def function():

  do stuff


works, but

object.attr.method=func

does not


I'm likely missing something, but I'd be curious to here experts opinions.

The library I am working with is:

https://github.com/j-hc/Reddit_ChatBot_Python

On 11/07/2022 18:23, Mats Wichmann wrote:
> On 7/11/22 04:58, nathan tech wrote:
>> As I understand it, decorators are usuallyed created as:
>> @object.event
>> Def function_to_be_executed():
>> Do_stuff
>>
>> My question is, is there a way to create this after the function is created?
>> So:
>> Def function():
>> Print("this is interesting stuff")
>>
>> @myobject.event=function
> You don't have to use the @ form at all, but your attempt to assign
> won't work.
>
> def function():
>      print("This is interesting stuff")
>
> function = object.event(function)
>
> Now the new version of function is the wrapped version; the original
> version (from your def statement) is held by a reference in the instance
> of the wrapper.
>
> If that's what you are asking...
>
> _______________________________________________
> Tutor maillist  -  Tutor at python.org
> To unsubscribe or change subscription options:
> https://nam12.safelinks.protection.outlook.com/?url=https%3A%2F%2Fmail.python.org%2Fmailman%2Flistinfo%2Ftutor&data=05%7C01%7C%7C8707660103e44e7ce3c908da6362eac3%7C84df9e7fe9f640afb435aaaaaaaaaaaa%7C1%7C0%7C637931573732838946%7CUnknown%7CTWFpbGZsb3d8eyJWIjoiMC4wLjAwMDAiLCJQIjoiV2luMzIiLCJBTiI6Ik1haWwiLCJXVCI6Mn0%3D%7C3000%7C%7C%7C&sdata=xfszgTLAm%2BXPKzIfJBwk6NqRWA%2Fmh4QpfUNlHwX97Po%3D&reserved=0
-- 

Best Wishes,

Nathan Smith, BSC


My Website: https://nathantech.net




More information about the Tutor mailing list