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

2

u/[deleted] Feb 21 '21

State importing, exporting in classes. No idea how the original develop did it.

Also abstract classes. I made one but not fully sure why.

Learning a lot of advanced oop on the job.

1

u/MaraudingAvenger Feb 21 '21

@ abstract classes - that's one area that another language might better allow you to understand -- since python is generally like, "welp, guess we'll do it that way" when you code something it doesn't really approve of.

abstract classes in C++ and interfaces in Java are both kind of what the abstract class in Python is. What you're doing is defining some structure that all the classes that implement that abstract class type will have in common.

in Java I might have an interface Shape { ... } that has a method in there that has no instructions in it at all, it's just a method name, and a return type:

public double getArea();

That means that any class that is made with that Shape interface is guarantee to contain a getArea() method that is public (I can call it from outside the class), and returns a double (float in python). Why does this matter? Because I'm going to make a Triangle class, a Square class, a Circle class, etc. and if I have a big collection of them, say for drawing something to the screen, I know I can call getArea() on all of them, because they all implement that Shape interface. In fact, Shape is one of their 'parent' classes, and I can use that type instead of Circle, Triangle, whatever to deal with them. Interfaces and abstract classes are like a contract; when you implement one, it's guaranteed to have certain features at a minimum.

Why it's not quite as easy in Python... well python doesn't care whether or not you implement the method you made in the abstract (interface) classes. It's just like, "well I guess they'll do that later" -- i.e. it's flexible, maybe to a fault. You can mess with that using the __subclasshook__ dunder method, There is a ton of functionality and info abstract base classes in the abc library. Check them docs!

Also you might want to check into mixins (basically standalone classes that contain implemented functions that you add to your classes to add specific functionality).