2

I am new working with classes in Python and I think I am misunderstanding something.

I am developing an application that allow the user to run a small test to ensure everything is OK. This is the code

## main.py ##

# Import libraries
from classes.test.test_control import TestingControl

user_input = sys.argv[1]

if user_input == 'run_test':
    # Run test control
    TestingControl()

## test_control.py

class TestingControl:
    def test_control_vcf()
    ...
# The class and the function don't need any argument

Do I really need a class to do this, or is it a good practice to use directly a function instead?

I have read the following link but still, it is not clear if in this example I should use it

When should I be using classes in Python?

(I could use directly the function, that's work but (apart from this is going to be evaluated) I want to learn.

  • Looking at your `main.py` code it looks like you just want to run a function. I would just write that function. – quamrana Jun 03 '22 at 20:09
  • Thanks!! The thing I am confused if because one my university teacher told me that "It's always a bad practice to run a function outside a class" So, for this, I have been trying to understand what is the class here for? – Manolo Dominguez Becerra Jun 03 '22 at 20:11
  • 4
    @ManuelDominguezBecerra your professor is wrong. Classes are one tool for building abstractions. They are not the only tool, and just making everything class based doesn't guarantee that you've created useful, re-usable abstractions. – juanpa.arrivillaga Jun 03 '22 at 20:14
  • 1
    'It is bad practice' should never translate to 'You can never do that'. – vultkayn Jun 03 '22 at 20:16
  • 1
    One thing you will come to understand is that a lot of programming is more like ideology than science. The wise programmer learns to be pragmatic and choose the good parts – juanpa.arrivillaga Jun 03 '22 at 20:16
  • 1
    @vultkayn sure, but it isn't bad practice – juanpa.arrivillaga Jun 03 '22 at 20:16
  • 1
    A lot of it is "cultural" too. You (usually) aren't writing code for yourself. You are writing code collaboratively with other people, and learning to follow the cultural practices of that group of programmers will make you a more effective contributor. – juanpa.arrivillaga Jun 03 '22 at 20:18

1 Answers1

1

This comes to a personal choice. I would recommend to watch Stop Writing Classes by Jack Diederich, Jack is a python core developer. I have attached a link to the youtube video. And at the end it's your call, whether you want a class or a function is fine.

https://youtu.be/o9pEzgHorH0

BhusalC_Bipin
  • 801
  • 3
  • 12