Take the 2-minute tour ×
Programmers Stack Exchange is a question and answer site for professional programmers interested in conceptual questions about software development. It's 100% free, no registration required.

In my code either of the above two options would work equally well but I am interested in finding out the pros and cons of both approaches.

share|improve this question
add comment

closed as not constructive by Martijn Pieters, Glenn Nelson, Yusubov, Robert Harvey, Dynamic Feb 22 '13 at 19:54

As it currently stands, this question is not a good fit for our Q&A format. We expect answers to be supported by facts, references, or expertise, but this question will likely solicit debate, arguments, polling, or extended discussion. If you feel that this question can be improved and possibly reopened, visit the help center for guidance.If this question can be reworded to fit the rules in the help center, please edit the question.

2 Answers

up vote 7 down vote accepted

It honestly depends on your needs. Most of the time a module of functions will be what you're looking for.

Python classes (imho) shouldn't be thought of like classes in C# or C++ or Java, there needs to be a logical reason to group a set of functions into a larger parent, data structures would be a good example of this, you want a congruent set of functions that apply directly to the class instead of a loosely collected set of functions that are general purpose.

  • Module of Functions: general purpose, utility, and more "global" functions
  • Module of Class of Functions: grouped classes of similar needs, different linked list data types or audio processing functions or different tree types.

A module is for the overall hierarchical grouping of similar systems, I usually have all utility functions under a single module, all data under another, my data layer and connectors under a more "parent" type module. Since a module is just a mental grouping it's less about what types of systems are in it (classes or functions, honestly probably a mix of the two) and more about the logical connections between the systems under the module.

share|improve this answer
1  
I don't understand the contrast between a Python class and other languages. In what language would you have a class with with functions that have no logical reason of being grouped? –  Guy Sirton Jan 26 '13 at 7:00
add comment

The basic question is if you need to have different instances of your module/class. a python module is like a singleton - you refer always to the same object, i.e. if you need instances, you need a class.

However, if you do not need instances you might still have the need to set up your "object" to an initial state. while this is clearly possible with a plain module, if the initializing is more complex than just setting some values, I would advice to have initializing code in a class's __init__ method for readability.

share|improve this answer
1  
it might be worth pointing out that you probably are talking about something that has state, e.g. a module with global variables vs a class with member variables. if your class only has methods but no state, there is little reason to have multiple instances –  thbusch Jan 26 '13 at 11:41
    
@thbusch, yes, this is implied in needing instances. –  kr1 Jan 26 '13 at 14:58
add comment

Not the answer you're looking for? Browse other questions tagged or ask your own question.