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?

5
  • 2
    You can have at most one instance of a module, and as many instances as you like of a class. They do different things
    – Caleth
    Dec 1, 2023 at 12:50
  • @Caleth Thank you. This is one valid reason to use classes. So (speaking in general) in absence of the need of more than one instance, do you suggest to use a module instead a class?
    – User051209
    Dec 1, 2023 at 13:08
  • 1
    I would use a module when there is an absence of any state, which is a stricter requirement than "more than one instance". The exception would be modules that are themselves runnable, i.e. they have if __name__ = "main": at module scope
    – Caleth
    Dec 1, 2023 at 13:17
  • To store a "state" I use a global variable in the module. Sometimes I have done this with success.
    – User051209
    Dec 1, 2023 at 13:22
  • 4
    You should avoid modules with mutable state. Even if you only have one instance right now, it's a good idea to wrap the state and behaviour in a class.
    – Caleth
    Dec 1, 2023 at 13:23

1 Answer 1

2

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.

6
  • Thank you very much. When I start to program by Python I have tried to program as in Java especially for the testing. In Java I have learnt to develop by TDD and I have used JUnit an Mockito. When I started with Python I create many classes by TDD and I tested them by the module unittest, but after some times I have understood that I coult test my code by unittest without the need of classes.
    – User051209
    Dec 2, 2023 at 17:37
  • The choice between OOP and procedural programming seems like a false dichotomy. You can use function references to do pretty much anything that you can do with OOP. You can also use OOP in Python without defining any classes. You can add properties and methods to any object, including functions.
    – JimmyJames
    Dec 6, 2023 at 20:43
  • @JimmyJames I will admit I am not well versed in advanced functional programming concepts, so perhaps there is a way to integrate classes into a function programming paradigm. However I am not sure that is the direction you are going - are you simply arguing that you can build a poor mans OOP using objects and functions in Python - if so I agree its possible, it may even be more readable for simple cases like currying arguments. However for more complex functionality, I am not sure what advantage you gain by not using classes.
    – DavidT
    Dec 6, 2023 at 21:17
  • @DavidT I don't argue that you 'gain' something by not using classes, per se. I'm just noting that you often don't need classes to achieve similar or equivalent results. That is, in many cases, implementing a class is overkill. A good example is filter(). You can absolutely build equivalent solutions using classes (e.g., in pre-1.8 Java) but it's unnecessary. There are a lot of useful solutions in the functools module, if you are interested and they don't preclude using classes.
    – JimmyJames
    Dec 6, 2023 at 22:04
  • I am considering editing my previous answer to remove Encapsulation, Abstraction and Static polymorphism, since I think you (@JimmyJames) are correct that these benefits are not unique to OOP (with respect to Python). However I think Inheritance and Dynamic Polymorphism still stand as benefits of OOP, thoughts?
    – DavidT
    Dec 6, 2023 at 22:16

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Not the answer you're looking for? Browse other questions tagged or ask your own question.