Python Forum
should I ... go class or stick with functions?
Thread Rating:
  • 0 Vote(s) - 0 Average
  • 1
  • 2
  • 3
  • 4
  • 5
should I ... go class or stick with functions?
#1
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?
Reply
#2
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.
Gribouillis likes this post
Reply
#3
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.
Gribouillis likes this post
If you can't explain it to a six year old, you don't understand it yourself, Albert Einstein
How to Ask Questions The Smart Way: link and another link
Create MCV example
Debug small programs

Reply
#4
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.
buran likes this post
Reply
#5
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.
Reply


Possibly Related Threads…
Thread Author Replies Views Last Post
  Calling functions from within a class: PYQT6 Anon_Brown 4 3,643 Dec-09-2021, 12:40 PM
Last Post: deanhystad
  Why built in functions are defined as class? quazirfan 5 2,714 Oct-23-2021, 01:20 PM
Last Post: Gribouillis
  using class functions in roguelike game trousers1 3 2,521 Dec-02-2019, 08:22 PM
Last Post: ichabod801

Forum Jump:

User Panel Messages

Announcements
Announcement #1 8/1/2020
Announcement #2 8/2/2020
Announcement #3 8/6/2020