2

Python is the language I use most in this period.

My background in Java

Before start learning Python I have programmed in Java language. In Java all code is written inside the methods of a class and variables are attributes of a class. In general is a best practice to avoid using static methods, public variables and so on.

Python is different from Java: encapsulation is difficult

When I started programming in Python I tried to do the same thing by writing code inside methods of a class and variables as attributes of a class. However, I realized that Python is different from Java in many aspects. For example, the concept of private variable or method is missing although particular names (__variable_name) can be used to hide attributes outside of the class. This makes encapsulation difficult.

After the initial period of Python programming, I have reduced the use of classes and I have started to write code and variable inside a simple Python module without create class.

Below I report an interesting comment that @JimmyJames has written in one other post:

My opinion is that with Python, you should use module level code as long as it meets your needs and isn't becoming spaghetti code and only introduce classes when they provide a clear benefit.

This is already a confirmation that in Python modules are preferable than classes, but I'm here to look for a most specific indication.

My question

In python when is absolutely preferable use a class than a module?

Doc Brown
  • 218,378

1 Answers1

3

Python with or without classes is Turing complete, so there isn't going to be any application that can only be written with or without them. Additionally python provides collections.namedtuple so if all you wanted was groups of attributes (equivalent of a C struct) you could use that instead.

Therefore you are really left with the distinction between procedural programming (C struct style) and full OOP (classes with methods being part of the class).

Therefore, I think your question just morphs into:

What are the benefits of OOP?

Listing the bullet points that is:

  • Encapsulation.
  • Abstraction.
  • Polymorphism
  • Inheritance (okay some people wouldn't call that a benefit)

In python when is absolutely preferable use a class than a module?

When one or more of the OOP attributes listed above, are useful for the problem you are trying to solve.


As a more practical matter I don't use classes much in python and when I do it's usually just a scoping tool: I have a bunch of long running state (that isn't global), I initialize a class and throw the state into member variables of the class. When methods in the class call other methods they don't need long parameter lists, since they can all just access the state at the class level.

I could achieve the same result simply by nesting functions inside other functions, but my history is similar to yours and nesting functions is not always the first solution I think of.

In python I have found I have to give a bit more thought to how I want to separate my code so that I can test it effectively. Thats probably a good thing - but in Java it's not something you have to give much thought to, due to the prevalence of Classes (everything is a class) and very good Mock and DI frameworks. In short in Java you just decide what you want to mock and the frameworks pretty much take care of it, without you having to change your production code.

DavidT
  • 4,601