Python Forum
should I ... go class or stick with functions? - Printable Version

+- Python Forum (https://python-forum.io)
+-- Forum: Python Coding (https://python-forum.io/forum-7.html)
+--- Forum: General Coding Help (https://python-forum.io/forum-8.html)
+--- Thread: should I ... go class or stick with functions? (/thread-30929.html)



should I ... go class or stick with functions? - 3Pinter - Nov-13-2020

Hi guys,

I have a bunch of functions which are 'bundled' in a __init__.py

The functions are basicly way to get standard data from our office.
Think of:
- what is the projectnumber
-----> no input needed. A program api query returns a value which is altered and returned.
- where to store pdf files based upon the age of a project
-----> projectnubmer as input for the function, based upon its value, a specific spot is returned.
- etc.etc.

Now, since I'm learning python myself, I'm looking at classes quite often and was wondering if the above is suited for a class?

I -think- I know what a class does, but hey, advice is much appreciated!


So I think:
MyProject.projectnumber()
MyProject.pdflocation()
MyProject.etcetc
would make more sense?


RE: should I ... go class or stick with functions? - michael1789 - Nov-13-2020

A class is an Object, or THING. A function is a list of directions. So the answer is no, you can't turn a function into a Class; however, you can make a class/object that has those functions as it's properties/methods.

So "where to store pdf files based upon the age of a project" becomes a data/file manger object.


RE: should I ... go class or stick with functions? - buran - Nov-13-2020

IMHO, Project looks like good candidate for a class. Probably some of your functions may fit as class methods, some of them may be "internal" or static ones and some of they may be better as utility functions - i.e. outside class.
You should distinguish between between e.g. Project.number property (i.e. that will be the number assigned to that project and some utility function that will be run probably once per project (when you have new project) and will generate the number that you will assign to the project. But this is just what I imagine is your workflow.
Note, we really don't have enough information to advise you better.


RE: should I ... go class or stick with functions? - atombear - Nov-13-2020

fwiw everything in python is an object, including functions. this isn't meant to be entirely pedantic, but i digress


there are generally 3 reasons to build a class

1) you want to bundle data with methods. if you find yourself passing the same argument to one function after another, it implies you want some context to hold that (meta)data. a class is a fine way to keep track of semi-globals in this way.
2) you want to use inheritance to reduce the amount of code you have.
3) you want to build a nice api - ie you want users to interact with the module in a very specific way.

there's really nothing wrong with keeping a library of functions.


RE: should I ... go class or stick with functions? - 3Pinter - Nov-14-2020

Thanks all for your much appreciated responses!

Based upon that I think a class would indeed be a proper way to code it.

As Atombear mentioned in bullet 1, functions are used in other functions. (projectnumber defines where to put data for example).

Class it is.