5

Let's say I have a python module that has a lot of functions that rely on each other, processing each others results. There's lots of cohesion.

That means I'll be passing back and forth a lot of arguments. Either that, or I'd be using global variables.

What are best practices to deal with such a situation if? Things that come to mind would be replacing those parameters with dictionaries. But I don't necessarily like how that changes the function signature to something less expressive. Or I can wrap everything into a class. But that feels like I'm cheating and using "pseudo"-global variables?

I'm asking specifically for how to deal with this in Python but I understand that many of those things would apply to other languages as well.

I don't have a specific code example right, it's just something that came to mind when I was thinking about this issue.

Examples could be: You have a function that calculates something. In the process, a lot of auxiliary stuff is calculated. Your processing routines need access to this auxiliary stuff, and you don't want to just re-compute it.

5
  • 1
    Introduce Parameter Object.
    – Peter Wood
    Oct 8, 2015 at 18:36
  • When you need to transfer many variables, your functions are not specific enough. Redesign your functions.
    – Daniel
    Oct 8, 2015 at 18:36
  • 1
    If you really want useful advice, you should show some code. At least the signatures of a couple of functions. The right way to avoid the issue, or even the legitimacy of trying to avoid it in the first place depends a lot on the meaning and semantics of the data you pass around.
    – spectras
    Oct 8, 2015 at 18:43
  • 1
    Followed by Move Method
    – Peter Wood
    Oct 8, 2015 at 19:13
  • You are over thinking it. A class is the correct approach. A class is not "global" in any way. Each object that instantiates the class has its own set of attributes, so they are inherently local to the object.
    – RobertB
    Jan 4, 2018 at 18:35

2 Answers 2

6

This is a very generic question so it is hard to be specific. What you seem to be describing is a bunch of inter-related functions that share data. That pattern is usually implemented as an Object.

Instead of a bunch of functions, create a class with a lot of methods. For the common data, use attributes. Set the attributes, then call the methods. The methods can refer to the attributes without them being explicitly passed as parameters.

0

As RobertB said, an object seems the clearest way. Could be as simple as:

class myInfo:
    def __init__(self, x=0.0, y=0.0):
        self.x = x
        self.y = y
        self.dist = self.messWithDist()

    def messWithDist(self):
        self.dist = math.sqrt(self.x*self.x + self.y*self.y)

blob = myInfo(3,4)
blob.messWithDist()
print(blob.dist)
blob.x = 5
blob.y = 7
blob.messWithDist()
print(blob.dist)

If some of the functions shouldn't really be part of such an object, you can just define them as (non-member, independent) functions, and pass the blob as one parameter. For example, by un-indenting the def of messWithDist, then calling as messWithDist(blob) instead of blob.messWithDist().

-s

2
  • You should set the attribute with __init__-arguments. Methods should return a value or change a attribute, not both.
    – Daniel
    Oct 8, 2015 at 18:54
  • True; thought of that but decided to keep it short. Will edit.
    – TextGeek
    Oct 8, 2015 at 19:15

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge that you have read and understand our privacy policy and code of conduct.

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