r/learnpython • • Feb 20 '21

What are you having difficulty learning or are currently struggling to learn?

I'm curious what the community here has difficulty with when it comes to learning Python. Partly because I want to see if the more experienced community here (or myself) can help point you into the right direction. . . but also because I'm trying to come up with video tutorial ideas for future content and would love to hear from Python learners what would be most useful to dedicate our time on.

I'm looking forward to hearing what you guys have to say!

417 Upvotes

324 comments sorted by

View all comments

22

u/Ijustlikepotatoes Feb 20 '21

Goddamn decorators...

I don't get it one bit. I've used it in flask a bit but honestly, I have no clue on how exactly it works.

FeelsConfusedMan

7

u/keraj93 Feb 20 '21

14

u/Ijustlikepotatoes Feb 20 '21

Me when I read python code:

"Mhm yes, oh yeah that. Yep I know some of these words"

Me when I see the @ :

"HnnnngksjdjdkddokdAaaaAwtf"

3

u/keraj93 Feb 20 '21

decorator simply add some functionality to functions

it is for example common with webframeworks like django. Webpages are basically defined by functions. When you want to restrict the access to a webpage only to authenticated users you simply add @login_required above the function. You can also add arguments to decoraters. with the example of @login_required, you can add an argument that defines where a non-authenticated user is redirected to because he/she has no access to the page.

2

u/Ijustlikepotatoes Feb 20 '21

Oh yeah I understood that in a flask/django setting. But anything outside of it makes things confusing.

Then there is decorators like @classmethod and such...

Ive got a lot to learn :')

7

u/dupelize Feb 20 '21

Decorators just wrap a function. That means they take the function as an argument, do something, call the function, do something, and return. Depending on the decorator one of those "do somethings" might be nothing.

If you have something that you might want to do to modify a bunch of different functions, a decorator might be the way to do it.

If used sparingly, they're a great tool. IMO, flask over does it a bit.

3

u/NightSkyth Feb 20 '21

And what's the point? Is it something like you don't want to modify directly your function by being too intrusive for instance?

6

u/Moikle Feb 21 '21

possibly something like a certain behaviour that you want to happen before and/or after multiple functions, and you don't want to have to rewrite it for each.

They are great for debugging. Maybe you want something to time how long functions take without actually modifying the function itself.

Really though, the trick is to learn how they work, and a couple of tricks you can do with them, and suddenly you will start coming up with your own uses for them.

3

u/groovitude Feb 21 '21

Is it something like you don't want to modify directly your function by being too intrusive for instance?

That's a good example of a use case. I think logging is a pretty ideal example of where you'd want to use a decorator. It enables you to have a single source of truth for how you do your logging -- if you decide to change something about how you log, you only have to change it in one place and not search all over for every example. It also keeps the logic for doing the logging out of the rest of the logic, keeping your functions clear and focused.

3

u/totemcatcher Feb 21 '21

One simple use case is to allow a function to support a variation of arguments. A function which takes an integer can be decorated to take a list. The decorator definition breaks the list into integers and repeatedly calls the decorated function with each index of the list.

1

u/NightSkyth Feb 21 '21

Oh, good idea. It could be very useful actually.

1

u/sje46 Feb 20 '21

I get decorators, but I hardly ever use them in my own code unless a library specifically requires that I use them, so I always have to look them up again.

I honestly just find them unnecessary 95% of the time. They get easier as you go along. discord.py uses them and is actually pretty easy to understand, so maybe you can create a discord bot to practice them

1

u/NewbornMuse Feb 21 '21 edited Feb 21 '21
  1. Functions are first-class citizens in python. That means they can be stored in variables, passed as arguments to functions, returned by functions, and so on. If I use a function without parentheses, I mean the function itself. If I use parentheses, I am calling the function and I mean the result. abs(4) is 4. abs(-6) is 6. abs is the absolute value function. I can easily do

    x = abs x(-6)

  2. A decorator is a function that takes in a function and returns a new, modified function.

  3. The syntax

.

@decorator
def func(foo, bar):
  #blabla

is exactly the same as:

def func(foo, bar):
  #blabla
func = decorator(func)

You define a function the usual way, then you immediately pass it to decorator, which returns you a modified version of it, and store that in the variable func again. The function func is now not just the block you defined, but whatever the decorator returns after being called on the block.

A toy non-example: You want a certain function to print a message when it returns 0. That is not what you'd use decorators for. You'd just check if the about-to-be-returned value is 0, print if yes, then return the value either way.

A toy example: I want several functions to print a message when they return 0. Instead of writing the boilerplate if output == 0: print("...") every time, I separate it into a decorator. How do I do that?

Remember that the decorator takes in the raw function and returns a new, modified function. So inside the decorator we define the new function and eventually return that. So virtually all decorators look like this:

def message_if_0(func): #func is what we are modifying
  def modified_func_to_be_returned(*args, **kwargs):
    #whatever your decorator does
  #I am done defining the function, it now exists as a variable, so I can...
  return modified_func_to_be_returned

In our case:

def message_if_0(func):
  def mftbr(*args, **kwargs):
    original_functions_output = func(*args, **kwargs)
    if original_functions_output == 0: print("oopsie")
    return original_functions_output
  #now we have defined mftbr, now we can return it
  return mftbr

And now you can use it like so:

@message_if_0
def very_important_function(x):
  return x + 2

1

u/Lewistrick Feb 21 '21

Yeah same! I can use them from the moment they come up in example code. But I never created one myself because I just can't figure out why/when I would do that and what it would bring me.