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.

7
  • 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. Commented Jun 3, 2022 at 20:14
  • 1
    'It is bad practice' should never translate to 'You can never do that'. Commented Jun 3, 2022 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 Commented Jun 3, 2022 at 20:16
  • 1
    @vultkayn sure, but it isn't bad practice Commented Jun 3, 2022 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. Commented Jun 3, 2022 at 20:18

1 Answer 1

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

Sign up to request clarification or add additional context in comments.

Comments

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.