Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Python - Should I put my helper functions inside or outside the class? [closed]

In Python, if some methods of a class need a helper function, but the helper function itself doesn't use anything in the class, should I put the helper function inside or outside the class?

I tried putting it inside but PyLint was complaining that this function could have been put outside.

@Karl:

The class is a software upgrader and the helper function creates a new folder if the folder doesn't exist yet. The class is in a module having pretty much only the code for the class as of now. Other classes may be added later on.

like image 809
Jack Z Avatar asked Oct 21 '11 21:10

Jack Z


People also ask

Where do you put helper functions in Python?

When deciding where to put helper functions the question I ask is, "Is it only for this class?" If it can help in other places, then it goes at the module level; if it is indeed only for this class, then it goes in the class with either staticmethod (needs no class data to do its job) or classmethod (uses some class, ...

Should helper functions be in a class?

In PHP you can either put the relevant functions into a nampespace or within a class (inside a namespace). It's up to you, there's no right or wrong approach.

Should helper functions go above or below?

According to Clean Code you should order your methods from top to bottom, in the order they are called. So it reada like a poem. I try to follow this advice, and it works quite well for me.

What is helper class python?

In object-oriented programming, a helper class is used to assist in providing some functionality, which isn't the main goal of the application or class in which it is used. An instance of a helper class is called a helper object (for example, in the delegation pattern).


1 Answers

When deciding where to put helper functions the question I ask is, "Is it only for this class?" If it can help in other places, then it goes at the module level; if it is indeed only for this class, then it goes in the class with either staticmethod (needs no class data to do its job) or classmethod (uses some class, but not instance, data to do its job).

Another python code checker is pyflakes.

like image 100
Ethan Furman Avatar answered Sep 19 '22 23:09

Ethan Furman