[Tutor] Question about python decorators

avi.e.gross at gmail.com avi.e.gross at gmail.com
Mon Jul 11 17:42:39 EDT 2022


Nathan.

I don't think anyonbe suggested you type what you say you did:

	object.attr.method=func

You are making a second pointer to your function which leaves everything unchanged.

The suggestion I think several offered was to call a function called object.attr.method and give it an object which is the function you want decorated, meaning "func" above, and CAPTURE the result as either a new meaning for the variable NAME called "func" or perhaps use a new handle.

The way it works is that not only does object.attr.method accept a function name as an argument but also creates  brand new function that encapsulates it and RETURNS it. You want the brand new function that is an enhancement over the old one. The old one is kept alive in the process, albeit maybe not reachable directly.

You may be confused by the way decorating is done as syntactic sugar where it is indeed hard to see or understand what decorating means, let alone multiple levels as all you SEE is:

@decorator
Function definition.

So until you try the format several have offered, why expect your way to work ...?

Again, you want to try:

result = object.attr.method(func)

or to re-use the name func:

func = object.attr.method(func)




-----Original Message-----
From: Tutor <tutor-bounces+avi.e.gross=gmail.com at python.org> On Behalf Of Nathan Smith
Sent: Monday, July 11, 2022 5:30 PM
To: tutor at python.org
Subject: Re: [Tutor] Question about python decorators

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


_______________________________________________
Tutor maillist  -  Tutor at python.org
To unsubscribe or change subscription options:
https://mail.python.org/mailman/listinfo/tutor



More information about the Tutor mailing list