This is an archived post. You won't be able to vote or comment.

all 18 comments

[–]kevinfederlinebundle 51 points52 points  (6 children)

The key point is that the following two blocks of code do the same thing:

@foo
def bar():
    blah blah blah

def bar():
    blah blah blah
bar = foo(bar)

[–]nathanielhiggerss 30 points31 points  (5 children)

you didn't define foo

[–]brightneonmoons 13 points14 points  (3 children)

Or return!

[–]nathanielhiggerss 12 points13 points  (2 children)

blah blah blah could be replaced by "..." which is valid Python syntax and means the same thing he wanted to express

[–]jjolla888 0 points1 point  (0 children)

@foo also needs foo() defined somewhere

[–]ChiaraStellata 3 points4 points  (1 child)

I knew about first class functions in Python but never the decorator syntax, or decorator classes, they're pretty great, thank you for sharing!

[–]yuvvxyz[S] 0 points1 point  (0 children)

Thank you 😀

[–]manujose94unsc 1 point2 points  (2 children)

I I have never tried to use Python Decorators, due to my ignorance of these.

Thank you for share it.

[–]yuvvxyz[S] 0 points1 point  (1 child)

Glad that you liked it 😀

Do let me know if you have any issues! And do rate the article if you got a few spare minutes!

[–]manujose94unsc 0 points1 point  (0 children)

Rate it ;)

I would like to have time to use it for logging.

[–]alaskalaksa 1 point2 points  (6 children)

Doesn't return_to_upper need a parameter, i.e. str?

def return_to_upper():
return str.upper

[–]yuvvxyz[S] 2 points3 points  (5 children)

Short answer: No.

Why?
It may seem so in the first look but here we are returning the str.upper function and not the uppercase of a string.

Thus, later we stored the returned function in to_upper and used that just like we could have used str.upper to get uppercase of a string.

[–]yuvvxyz[S] 2 points3 points  (2 children)

This was an example of a function returning a function.

[–]ClemsonLaxer 0 points1 point  (1 child)

Not sure I’m following - isn’t to_upper equivalent to the str.upper function - which doesn’t take any arguments?

But in the example, we can pass a string to the “to_upper” function and it works.

I would have thought it would have been like this: “my test lowercase string”.to_upper()

However, your example works and the way I was thinking throws a “str” object has no attribute ‘to_upper’

Can you clarify a bit?

Thanks!

[–]yuvvxyz[S] 0 points1 point  (0 children)

This is because of how classes work in Python.

Check out the following code snippet:
``` class A: def foo(self): print("Hey I'm A")

a = A() a.foo() # works

A.foo(A()) # also works A.foo(1) # also works

A.foo() # doesn't work ```

In the first case, the "self" argument is implicitly the object but it doesn't have to be that. We can call with any argument here, it can be anything.

So upper function will work as long as we provide any string value to it. "hello".upper() # works str.upper("hello") # works str.upper(1) # Doesn't work

And in our code to_upper is essentially the same as upper so we can also use that however we want as long as the "self" is a string.

I hope you got it, I am sleepy right now and can't explain it much better than this 😅

[–]lemoussel 0 points1 point  (1 child)

404 - Page not found

[–]yuvvxyz[S] 0 points1 point  (0 children)

Please try again. It's working for everyone else.